home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / tpu55a.zip / TPUDOC1.TXT < prev    next >
Text File  |  1990-08-11  |  133KB  |  2,836 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.                            -----------------------------
  18.  
  19.  
  20.  
  21.                            INSIDE TURBO PASCAL 5.5 UNITS
  22.  
  23.  
  24.  
  25.                            -----------------------------
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.                                          by
  50.  
  51.                                   William L. Peavy
  52.  
  53.                                  -----------------
  54.  
  55.                               Revised: August 11, 1990
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.                                       ABSTRACT
  63.  
  64.             This document  provides a  revised report on researches into
  65.             the structure  and content  of Unit (.TPU) files produced by
  66.             Turbo Pascal  (version 5.5)  from Borland International.  No
  67.             assurances are  possible regarding  when (if  ever)  further
  68.             updates will be available so the material is released to the
  69.             Turbo Pascal  user community  in its  admittedly  imcomplete
  70.             state since  very little of consequence really remains to be
  71.             done.
  72.  
  73.  
  74.  
  75.                                       COMMENTS
  76.  
  77.             Comments  and   feed-back  are  welcome  --  especially  new
  78.             contributions.  I can be reached via the following services:
  79.  
  80.                  CompuServ           (70042,2310)
  81.  
  82.                  HalPC Telecom-1     (William;Peavy)
  83.  
  84.                  HalPC Telecom-2     (Wm;Peavy)
  85.  
  86.  
  87.  
  88.                                   Table Of Contents
  89.  
  90.  
  91.  
  92.            Introduction ................................................ 3
  93.  
  94.            1. Gross File Structure ..................................... 3
  95.                1.1 User Units .......................................... 4
  96.  
  97.            2. Locators ................................................. 5
  98.                2.1 Local Links ......................................... 5
  99.                2.2 Global Links ........................................ 5
  100.                2.3 Table Offsets ....................................... 5
  101.  
  102.            3. Unit Header .............................................. 6
  103.                3.1 Description ......................................... 6
  104.                3.2 File Size ........................................... 9
  105.  
  106.            4. Symbol Dictionaries ...................................... 9
  107.                4.1 Organization ........................................ 9
  108.                4.2 Interface Dictionary ............................... 10
  109.                4.3 DEBUG Dictionary ................................... 10
  110.  
  111.                4.4 Dictionary Elements ................................ 10
  112.                    4.4.1 Hash Tables .................................. 10
  113.                        4.4.1.1 Size ................................... 11
  114.                        4.4.1.2 Scope .................................. 12
  115.                        4.4.1.3 Special Cases .......................... 12
  116.  
  117.                    4.4.2 Dictionary Headers ........................... 13
  118.  
  119.                    4.4.3 Dictionary Stubs ............................. 13
  120.                        4.4.3.1 Label Declaratives ("O") ............... 13
  121.                        4.4.3.2 Un-Typed Constants ("P") ............... 14
  122.                        4.4.3.3 Named Types ("Q") ...................... 14
  123.                        4.4.3.4 Variables, Fields, Typed Cons ("R") .... 15
  124.                        4.4.3.5 Subprograms & Methods ("S") ............ 16
  125.                        4.4.3.6 Turbo Std Procedures ("T") ............. 17
  126.                        4.4.3.7 Turbo Std Functions ("U") .............. 17
  127.                        4.4.3.8 Turbo Std "NEW" Routine ("V") .......... 17
  128.                        4.4.3.9 Turbo Std Port Arrays ("W") ............ 17
  129.                        4.4.3.10 Turbo Std External Variables ("X") .... 17
  130.                        4.4.3.11 Units ("Y") ........................... 18
  131.  
  132.                    4.4.4 Type Descriptors ............................. 19
  133.                        4.4.4.1 Scope .................................. 19
  134.                        4.4.4.2 Prefix Part ............................ 20
  135.  
  136.                        4.4.4.3 Suffix Parts ........................... 21
  137.                            4.4.4.3.1 Un-Typed ......................... 21
  138.                            4.4.4.3.2 Structured Types ................. 22
  139.                                4.4.4.3.2.1 ARRAY Types ................ 22
  140.                                4.4.4.3.2.2 RECORD Types ............... 22
  141.                                4.4.4.3.2.3 OBJECT Types ............... 23
  142.                                4.4.4.3.2.4 FILE (non-TEXT) Types ...... 23
  143.                                4.4.4.3.2.5 TEXT File Types ............ 23
  144.                                4.4.4.3.2.6 SET Types .................. 24
  145.  
  146.  
  147.  
  148.                                         - i -
  149.  
  150.  
  151.  
  152.                                   Table Of Contents
  153.  
  154.  
  155.                                4.4.4.3.2.7 POINTER Types .............. 24
  156.                                4.4.4.3.2.8 STRING Types ............... 24
  157.  
  158.                            4.4.4.3.3 Floating-Point Types ............. 24
  159.  
  160.                            4.4.4.3.4 Ordinal Types .................... 24
  161.                                4.4.4.3.4.1 "Integers" ................. 25
  162.                                4.4.4.3.4.2 BOOLEANs ................... 25
  163.                                4.4.4.3.4.3 CHARs ...................... 25
  164.                                4.4.4.3.4.4 ENUMERATions ............... 26
  165.  
  166.                            4.4.4.3.5 SUBPROGRAM Types ................. 26
  167.  
  168.            5. Maps and Lists .......................................... 27
  169.                5.1 PROC Map ........................................... 27
  170.                5.2 CSeg Map ........................................... 28
  171.                5.3 Typed CONST DSeg Map ............................... 28
  172.                5.4 Global VAR DSeg Map ................................ 29
  173.                5.5 Donor Unit List .................................... 29
  174.                5.6 Source File List ................................... 30
  175.                5.7 DEBUG Trace Table .................................. 31
  176.  
  177.            6. Code, Data, Relocation Info ............................. 32
  178.                6.1 Object CSegs ....................................... 32
  179.                6.2 CONST DSegs ........................................ 32
  180.                6.3 Relocation Data Table .............................. 33
  181.  
  182.            7. Supplied Program ........................................ 34
  183.                7.1 TPUNEW ............................................. 35                                                                           |
  184.                7.2 TPURPT1 ............................................ 35
  185.                7.3 TPUAMS1 ............................................ 35
  186.                7.4 TPUUNA1 ............................................ 35
  187.                7.5 Modifications ...................................... 36
  188.  
  189.                7.6 Notes on Program Logic ............................. 36                                                                           |
  190.                    7.6.1 Formatting the Dictionary .................... 37                                                                           |
  191.                    7.6.2 The Disassembler ............................. 38                                                                           |
  192.  
  193.            8. Unit Libraries .......................................... 41
  194.                8.1 Library Structure .................................. 41
  195.                8.2 The TPUMOVER Utility ............................... 41
  196.  
  197.            9. Application Notes ....................................... 41
  198.  
  199.            10. Acknowledgements ....................................... 42
  200.  
  201.            11. References ............................................. 43
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.                                        - ii -
  213.  
  214.  
  215.  
  216.                            Inside TURBO Pascal 5.5 Units           
  217.        ----------------------------------------------------------------------
  218.  
  219.        INTRODUCTION
  220.  
  221.  
  222.        This document  is  the  outcome  of  an  inquiry  conducted  into  the
  223.        structure and  content of  Borland Turbo  Pascal  (Version  5.5)  Unit
  224.        files.   The original  purpose of the inquiry was to provide a body of
  225.        theory enabling  Cross-Reference programs  to  resolve  references  to
  226.        symbols defined  in .TPU  files where qualification was not explicitly
  227.        provided.   As is  so often the case, one thing led to another and the
  228.        scope of  the inquiry  was expanded dramatically.  While this document
  229.        should not be regarded as definitive, the author feels that the entire
  230.        Turbo Pascal  User community might gain from the information extracted
  231.        from these files at the cost of so much time and effort.
  232.  
  233.        The  material   contained   herein   represents   the   findings   and
  234.        interpretations of  the author.    A  great  deal  of  guess-work  was
  235.        required and  no assurances are given as to the accuracy of either the
  236.        findings of fact or the inferences contained herein which are the sole
  237.        work-product of the author.  In particular, the author had access only
  238.        to materials  or information  that any  normal  Borland  customer  has
  239.        access to.   Further,  no Borland  source-codes were  available as the
  240.        Library Routine source is not licensed to the author.  In short, there
  241.        was nothing irregular about how these findings were achieved.
  242.  
  243.        The material  contained herein  is placed in the public domain free of
  244.        copyright for  use of  the general public at its own risk.  The author
  245.        assumes no  liability for  any damages  arising from  the use  of this
  246.        material by  others.   If you make use of this information and you get
  247.        burned, TOUGH!   The  author accepts no obligation to correct any such
  248.        errors as  may exist  in the  supplied programs  or in the findings of
  249.        fact or  opinion contained  herein.   On the other hand, this is not a
  250.        "complete" work in that a great many questions remain open, especially
  251.        as regards  fine details.   (The author is not a practitioner of Intel
  252.        80xxx Assembly  Language and  several open  questions  might  best  be
  253.        addressed by persons competent in this area.)  The author welcomes the
  254.        input of  interested readers  who might be able to "flesh-out" some of
  255.        these open questions with "hard" answers.
  256.  
  257.  
  258.        1. GROSS FILE STRUCTURE
  259.  
  260.  
  261.        A Turbo  Pascal Unit  file (Version  5.5 only) consists of an array of
  262.        bytes that  is some  exact multiple  of  sixteen  (16).    "Signature"
  263.        information allows  the compiler  to verify  that the  .TPU  file  was
  264.        compiled with the correct compiler version and to verify that the file
  265.        is of  the correct  size.   The fine  structure of  the file  will  be
  266.        addressed in later sections at ever increasing levels of detail.
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.        ----------------------------------------------------------------------
  276.        Rev: August 11, 1990                                            Page 3
  277.  
  278.  
  279.  
  280.                            Inside TURBO Pascal 5.5 Units           
  281.        ----------------------------------------------------------------------
  282.  
  283.        Graphically, the  file may be regarded as having the following general
  284.        layout:
  285.  
  286.              +-------------------+
  287.              | Unit Header       |        Main Index to Unit File
  288.              +-------------------+
  289.              | Dictionaries:     |
  290.              |   a) Interface    |
  291.              |   b) Debugger   * |        For Local Symbol Access
  292.              +-------------------+
  293.              | PROC Map          |
  294.              +-------------------+
  295.              | CSeg Map        * |        May be Empty
  296.              +-------------------+
  297.              | CONST DSeg Map  * |        May be Empty
  298.              +-------------------+
  299.              | VAR DSeg Map    * |        May be Empty
  300.              +-------------------+
  301.              | Donor Units     * |        May be Empty
  302.              +-------------------+
  303.              | Source Files      |
  304.              +-------------------+
  305.              | Trace Table     * |        May be Empty
  306.              +-------------------+
  307.              | CODE Segment(s) * |        May be Empty
  308.              +-------------------+
  309.              | DATA Segment(s) * |        May be Empty
  310.              +-------------------+
  311.              | RELO Data       * |        May be Empty
  312.              +-------------------+
  313.  
  314.  
  315.        1.1 USER UNITS
  316.  
  317.  
  318.        Units prepared by the compiler available to ordinary users have a very
  319.        straight-forward appearance  and content.   There may even be a little
  320.        "wasted" space  that might  be removed  if the  compiler were  just  a
  321.        little cleverer.  The SYSTEM.TPU file is quite another thing however.
  322.  
  323.        The SYSTEM.TPU  file (found  in TURBO.TPL)  is extraordinary  in  that
  324.        great pains  seem to  have been  taken to  compact it.    Further,  it
  325.        contains a  great many  types of  entries that  just don't  seem to be
  326.        achievable by  ordinary users  and I suspect that much (if not all) of
  327.        it was "hand-coded" in Assembler Language.
  328.  
  329.        In the  following sections, the details of these optimizations will be
  330.        explained  in  the  context  of  the  structural  element  then  under
  331.        discussion.
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.        ----------------------------------------------------------------------
  340.        Rev: August 11, 1990                                            Page 4
  341.  
  342.  
  343.  
  344.                            Inside TURBO Pascal 5.5 Units           
  345.        ----------------------------------------------------------------------
  346.  
  347.        2. LOCATORS
  348.  
  349.  
  350.        The data  in these  files has  need of  structure and  organization to
  351.        support efficient access by the various programs such as the compiler,
  352.        the linker  and the  debugger.   This organization is built on a solid
  353.        foundation of locators employed in the unit's data structures.
  354.  
  355.  
  356.  
  357.        2.1 LOCAL LINKS
  358.  
  359.  
  360.        Local Links  (LL's) are  items of type WORD (2 bytes) which contain an
  361.        offset which  is relative to the origin of the unit file itself.  This
  362.        implies that  a unit must be somewhat less than 64K bytes in size.  If
  363.        the .TPU file is loaded into the heap, then LL's can be used to locate
  364.        any byte in the segment beginning with the load point of the file.
  365.  
  366.  
  367.  
  368.        2.2 GLOBAL LINKS
  369.  
  370.  
  371.        Global Links  (LG's) are  used to  locate type  descriptors which  may
  372.        reside in  other Units  (i.e., units  external to  the present  unit).
  373.        LG's are  structured items  consisting of two (2) words.  The first of
  374.        these is  an LL  that is  relative to  the origin  of  the  (possibly)
  375.        external unit.  The second word is an LL which locates the stub of the
  376.        unit entry  in the current unit dictionary for the (possibly) external
  377.        unit.   This dictionary  entry provides  the name  of  the  unit  that
  378.        contains the item the LG points to.
  379.  
  380.        This provides  a handy  mechanism for  locating type descriptors which
  381.        are defined in other separately compiled units.
  382.  
  383.  
  384.  
  385.        2.3 TABLE OFFSETS
  386.  
  387.  
  388.        Finally, various  data-structures within  a .TPU file are organized as
  389.        arrays of fixed-length records or as lists of variable-length records.
  390.        Efficient access  to such  records is  achieved by  means  of  offsets
  391.        rather than subscripts (an addressing technique denied Pascal).  These
  392.        offsets are  relative to  the  origin  of  the  array  or  list  being
  393.        referenced rather than the origin of the unit.
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403.        ----------------------------------------------------------------------
  404.        Rev: August 11, 1990                                            Page 5
  405.  
  406.  
  407.  
  408.                            Inside TURBO Pascal 5.5 Units           
  409.        ----------------------------------------------------------------------
  410.  
  411.        3. UNIT HEADER
  412.  
  413.  
  414.        The Unit  Header comprises  the first  64 bytes  of the .TPU file.  It
  415.        contains LL's  that effectively  locate all other sections of the .TPU
  416.        file plus  statistics  that  enable  a  little  cross-checking  to  be
  417.        performed.   Some parts  of the  Unit Header appear to be reserved for
  418.        future use  since no  unit examined  by this author has ever contained
  419.        non-zero data in these apparently reserved fields.
  420.  
  421.  
  422.  
  423.        3.1 DESCRIPTION
  424.  
  425.  
  426.        The Unit Header provides a high-level locator table whereby each major
  427.        structure in the unit file can be addressed.  The following provides a
  428.        Pascal-like explanation  of the  layout  of  the  header  followed  by
  429.        further narrative  discussion of the contents of the individual fields
  430.        in the Unit Header.
  431.  
  432.              Type HdrAry = Array[0..3] of Char; LL = Word;
  433.  
  434.              UnitHeader = Record
  435.  
  436.               FilHd : HdrAry;  { +00 : = 'TPU6'                       }
  437.               Fillr : HdrAry;  { +04 : = $00000000                    }
  438.               UDirE : LL;      { +08 : to Dictionary Head-This Unit   }
  439.               UGHsh : LL;      { +0A : to Interface Hash Header       }
  440.               UHPrc : LL;      { +0C : to PROC Map                    }
  441.               UHCsg : LL;      { +0E : to CSeg Map                    }
  442.               UHDsT : LL;      { +10 : to DSeg Map-Typed CONST's      }
  443.               UHDsV : LL;      { +12 : to DSeg Map-GLOBAL Variables   }
  444.               URULt : LL;      { +14 : to Donor Unit List             }
  445.               USRCF : LL;      { +16 : to Source file List            }
  446.               UDBTS : LL;      { +18 : to Debug Trace Step Controls   }
  447.               UndNC : LL;      { +1A : to end non-code part of Unit   }
  448.               ULCod : Word;    { +1C : Size of Code                   }
  449.               ULTCon: Word;    { +1E : Size of Typed Constant Data    }
  450.               ULPtch: Word;    { +20 : Size of Relo Patch List        }
  451.               Unknx : Word;    { +22 : Number of Virtual Objects???   }
  452.               ULVars: Word;    { +24 : Size of GLOBAL VAR Data        }
  453.               UHash2: LL;      { +26 : to Debug Hash Header           }
  454.               UOvrly: Word;    { +28 : Number of Procs to Overlay??   }
  455.               UVTPad: Array[0..10]
  456.                       of Word; { +2A : Reserved for Future Expansion? }
  457.  
  458.              End; { UnitHeader }
  459.  
  460.          FilHd   contains the  characters "TPU6"  in that  order.   This is
  461.                  clear evidence that this unit was compiled by Turbo Pascal
  462.                  Version 5.5.
  463.  
  464.          Fillr   is apparently reserved and contains binary zeros.
  465.  
  466.  
  467.        ----------------------------------------------------------------------
  468.        Rev: August 11, 1990                                            Page 6
  469.  
  470.  
  471.  
  472.                            Inside TURBO Pascal 5.5 Units           
  473.        ----------------------------------------------------------------------
  474.  
  475.          UDirE   contains an  LL (WORD)  which  points  to  the  Dictionary
  476.                  Header in which the name of this unit is found.
  477.  
  478.          UGHsh   contains an LL (WORD) which points to a Hash table that is
  479.                  the root of the Interface Dictionary tree.
  480.  
  481.          UHPrc   contains an  LL (WORD)  which points  to the  PROC Map for
  482.                  this unit.   The  PROC Map  contains  an  entry  for  each
  483.                  Procedure or  Function declared  in the  unit (except  for
  484.                  INLINE types),  plus an  entry for the Unit Initialization
  485.                  section.   The length  of  the  PROC  Map  (in  bytes)  is
  486.                  determined by subtracting this LL (at 000C) from the LL at
  487.                  offset 000E.
  488.  
  489.          UHCsg   contains an  LL (WORD)  which points  to  the  CSeg  (CODE
  490.                  Segment) Map  for this  unit.   The CSeg  Map contains  an
  491.                  entry for  each CODE Segment produced by the compiler plus
  492.                  an entry  for each  of the  CODE Segments included via the
  493.                  {$L filename.OBJ}  compiler directive.  The length of this
  494.                  Map (in  bytes) is  obtained by  subtracting this  LL  (at
  495.                  000E) from  the word  at 0010.   The result may be zero in
  496.                  which case the CSeg Map is empty.
  497.  
  498.          UHDsT   contains an  LL (WORD)  which points  to  the  DSeg  (DATA
  499.                  Segment) Map  that maps  the initializing  data for  Typed
  500.                  CONST items  plus  templates  for  VMT's  (Virtual  Method
  501.                  Tables) that  are associated  with  OBJECTS  which  employ
  502.                  Virtual Methods.   The  length of  this Map  (in bytes) is
  503.                  obtained by subtracting this LL (at 0010) from the word at
  504.                  0012.   The result may be zero in which case this DSeg Map
  505.                  is empty.
  506.  
  507.          UHDsV   contains an  LL (WORD)  which points  to  the  DSeg  (DATA
  508.                  Segment) Map  that contains  the specifications  for  DSeg
  509.                  storage required  by VARiables whose scope is GLOBAL.  The
  510.                  length of  this Map  (in bytes) is obtained by subtracting
  511.                  this LL  (at 0012)  from the word at 0014.  The result may
  512.                  be zero in which case this DSeg Map is empty.
  513.  
  514.          URULt   contains an  LL (WORD)  which points  to a  table of units
  515.                  which contribute  either CODE or DATA Segments to the .EXE
  516.                  file for  a program  using this  Unit.  This is called the
  517.                  "Donor Unit  Table".   The length of this table (in bytes)
  518.                  is obtained by subtracting this LL (at 0014) from the word
  519.                  at 0016.   The result may be zero in which case this table
  520.                  is empty.
  521.  
  522.          USRCF   contains an  LL (WORD)  which points to a list of "source"
  523.                  files.   These are  the files  whose CODE or DATA Segments
  524.                  are included  in this  Unit by the compiler.  Examples are
  525.                  the Pascal Source for the Unit itself, plus the .OBJ files
  526.                  included via  the {$L  filename.OBJ}  compiler  directive.
  527.                  The length  of  this  table  (in  bytes)  is  obtained  by
  528.                  subtracting this  LL (at 0016) from the word at 0018.  The
  529.                  result may be zero in which case this table is empty.
  530.  
  531.        ----------------------------------------------------------------------
  532.        Rev: August 11, 1990                                            Page 7
  533.  
  534.  
  535.  
  536.                            Inside TURBO Pascal 5.5 Units           
  537.        ----------------------------------------------------------------------
  538.  
  539.          UDBTS   contains an  LL (WORD)  which points to a Trace Table used
  540.                  by the  DEBUGGER for  "stepping"  through  a  Function  or
  541.                  Procedure contained  in this  Unit.   The length  of  this
  542.                  table (in  bytes) is  obtained by  subtracting this LL (at
  543.                  0018) from  the word  at 001A.   The result may be zero in
  544.                  which case this table is empty.
  545.  
  546.          UndNC   contains an  LL (WORD) which points to the first free byte
  547.                  which follows  the Trace  Table (if  any).  It serves as a
  548.                  delimiter for  determinimg the  size of  the Trace  Table.
  549.                  This LL  (when rounded up to the next integral multiple of
  550.                  16) serves to locate the start of the code/data segments.
  551.  
  552.          ULCod   is a  WORD that  contains the total byte count of all CODE
  553.                  Segments compiled into this Unit.
  554.  
  555.          ULTCon  is a  WORD that contains the total byte count of all Typed
  556.                  CONST and VMT DATA Segments compiled into this unit.
  557.  
  558.          ULPtch  is a  WORD that  contains the  total  byte  count  of  the
  559.                  Relocation Data Table for this unit.
  560.  
  561.          Unknx   is a  WORD whose  usage is  poorly understood.  It appears
  562.                  always to  be zero  except when  the Unit contains OBJECTs
  563.                  which employ Virtual Methods.
  564.  
  565.          ULVars  is a WORD that contains the total byte count of all GLOBAL
  566.                  VAR DATA Segments compiled into this unit.
  567.  
  568.          UHash2  contains an  LL (WORD)  which points to a Hash Table which
  569.                  is the  root of the DEBUGGER Dictionary.  If Local Symbols
  570.                  were generated  by the compiler (directive {$L+}) then ALL
  571.                  symbols declared  in the  unit can  be accessed  from this
  572.                  Hash Table.   In  the SYSTEM.TPU  file, there  is no  such
  573.                  Dictionary and  the LL stored here points to the INTERFACE
  574.                  Dictionary.  This is an example of Hash Table "Folding" to
  575.                  save space which has been observed only in SYSTEM.TPU.
  576.  
  577.          UOvrly  is a  WORD whose usage is poorly understood.  This word is
  578.                  usually zero unless the Unit was compiled with the Overlay
  579.                  Directive {$O+}.
  580.  
  581.          UVTPad  begins a  series of  eleven (11) words that are apparently
  582.                  reserved for future use.  Nothing but zeros have ever been
  583.                  seen here by this author.
  584.  
  585.  
  586.  
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.        ----------------------------------------------------------------------
  596.        Rev: August 11, 1990                                            Page 8
  597.  
  598.  
  599.  
  600.                            Inside TURBO Pascal 5.5 Units           
  601.        ----------------------------------------------------------------------
  602.  
  603.        3.2 FILE SIZE
  604.  
  605.  
  606.        An independent  check on  the size of the .TPU file is available using
  607.        information contained  in the Unit Header.  This is also important for
  608.        .TPL (Unit  Library) organization.  To compute the file size, refer to
  609.        the four  (4) words  at offsets  001A, 001C, 001E and 0020.  Round the
  610.        contents of  each of  these words to the lowest multiple of 16 that is
  611.        greater than  or equal to the content of that word.  Then form the sum
  612.        of the rounded words.  This is the .TPU file size in bytes.
  613.  
  614.  
  615.  
  616.        4. SYMBOL DICTIONARIES
  617.  
  618.  
  619.        This area contains all available documentation of declared symbols and
  620.        procedure blocks  defined within  the unit.    Depending  on  compiler
  621.        options in  effect when  the unit  was  compiled,  this  section  will
  622.        contain at  a minimum,  the INTERFACE  declarations, and at a maximum,
  623.        ALL declarations.   The information stored in the dictionary is highly
  624.        dependent on  the context  of the  symbol declared.   We defer further
  625.        explanation to the appropriate section which follows.
  626.  
  627.  
  628.  
  629.        4.1 ORGANIZATION
  630.  
  631.  
  632.        The dictionary  is organized  with a Hash Table as its root.  The hash
  633.        table is  used to  provide rapid  access to  arbitrary symbols.  Since
  634.        Turbo Pascal  compiles very rapidly, I presume the hash function to be
  635.        worthwhile to say the least.
  636.  
  637.        The dictionary  itself may  be thought  of as  an n-way  tree.    Each
  638.        subtree has its roots in a hash table.  There may be a great many hash
  639.        tables in  a given unit and their number depends on unit complexity as
  640.        well as  the options  chosen when  the unit  was compiled.  Use of the
  641.        {$L+} directive  produces the  densest trees.   The  hash  tables  are
  642.        explained in detail a few sections further on.
  643.  
  644.        Hash tables  point to  Dictionary Headers.   When  two or more symbols
  645.        produce the  same hash  function result, a collision is said to occur.
  646.        Collisions  are  resolved  by  the  time-honored  method  of  chaining
  647.        together the  Dictionary Headers of those symbols having the same hash
  648.        function result.   Dictionary supersetting is accomplished using these
  649.        chains.
  650.  
  651.  
  652.  
  653.  
  654.  
  655.  
  656.  
  657.  
  658.  
  659.        ----------------------------------------------------------------------
  660.        Rev: August 11, 1990                                            Page 9
  661.  
  662.  
  663.  
  664.                            Inside TURBO Pascal 5.5 Units           
  665.        ----------------------------------------------------------------------
  666.  
  667.        4.2 INTERFACE DICTIONARY
  668.  
  669.  
  670.        The INTERFACE  dictionary  contains  all  symbols  and  the  necessary
  671.        explanatory data  for the  INTERFACE section  of a  Unit.  Symbols get
  672.        added to  the  Unit  using  increasing  storage  addresses  until  the
  673.        IMPLEMENTATION section is encountered.
  674.  
  675.  
  676.  
  677.        4.3 DEBUG DICTIONARY
  678.  
  679.  
  680.        The DEBUG  dictionary (if  present) is  a superset  of  the  INTERFACE
  681.        dictionary.   It is  used by  the Turbo  Debugger to  support its many
  682.        features when  tracing through a unit.  If present, this dictionary is
  683.        rooted in  its  own  hash  table.    The  hash  table  is  effectively
  684.        initialized when  the  IMPLEMENTATION  keyword  is  processed  by  the
  685.        compiler.   This takes  the form  (initially) of an unmodified copy of
  686.        the INTERFACE  hash table,  to which  symbols are  added in  the usual
  687.        fashion.   Thus, the  hash chains constructed or extended at this time
  688.        lead naturally to the INTERFACE chains and this is how the superset is
  689.        effectively implemented.
  690.  
  691.  
  692.  
  693.        4.4 DICTIONARY ELEMENTS
  694.  
  695.  
  696.        The dictionary contains four major elements.  These are:  hash tables,
  697.        Dictionary Headers,  Dictionary  Stubs  and  Type  Descriptors.    The
  698.        distinction  between  Dictionary  Headers  and  Stubs  is  essentially
  699.        arbitrary and  is made in this document to assist in exposition.  They
  700.        might just  as easily  be regarded as a single element (such as symbol
  701.        entry).
  702.  
  703.  
  704.  
  705.        4.4.1 HASH TABLES
  706.  
  707.  
  708.        As has  been intimated,  Hash Tables  are  the  glue  that  binds  the
  709.        dictionary entries  together and  gives the  dictionary  its  "shape".
  710.        They effectively  implement the  scope rules of the language and speed
  711.        access to essential information.
  712.  
  713.        Each Hash table begins with a 2-byte size descriptor.  This descriptor
  714.        contains the  number of bytes in the table proper (less 2).  Thus, the
  715.        descriptor directly  points to the last bucket in the hash table.  For
  716.        a hash  table of  128 bytes,  the size  descriptor contains  126.  The
  717.        first bucket in the table immediately follows the size descriptor.
  718.  
  719.  
  720.  
  721.  
  722.  
  723.        ----------------------------------------------------------------------
  724.        Rev: August 11, 1990                                           Page 10
  725.  
  726.  
  727.  
  728.                            Inside TURBO Pascal 5.5 Units           
  729.        ----------------------------------------------------------------------
  730.  
  731.        4.4.1.1 SIZE
  732.  
  733.  
  734.        So far,  three different  hash table  sizes have  been observed.   The
  735.        INTERFACE and  DEBUG hash tables are usually 128 bytes (64 entries) in
  736.        size plus  2 bytes  of size  description, but the SYSTEM.TPU unit is a
  737.        special case,  containing only  16 entries.   Hash tables which anchor
  738.        subtrees whose  scope is  relatively local  usually contain  four  (4)
  739.        entries (8 bytes).
  740.  
  741.        Graphically, a Hash Table with four slots has the following layout:
  742.  
  743.              +--------------------+
  744.              |       0006h        |      Size Descriptor
  745.              +====================+
  746.              |       slot 0       |      an LL or zero
  747.              +--------------------+
  748.              |       slot 1       |      an LL or zero
  749.              +--------------------+
  750.              |       slot 2       |      an LL or zero
  751.              +--------------------+
  752.              |       slot 3       |      an LL or zero
  753.              +--------------------+
  754.  
  755.        It should  be noted  that the Size Descriptor furnishes an upper bound
  756.        for the  hash function  itself.  Thus, it seems possible that a single
  757.        hash function is used for all hash tables and that its result is ANDed
  758.        with the  Size Descriptor  to get the final result.  Because the sizes
  759.        are chosen  as they  are (powers of 2) this is feasible.  Note that in
  760.        the above  example, 6  = 2 * (n - 1) where n = 4 {slot count}.  All of
  761.        the hash tables observed so far have this property.  What you get is a
  762.        really efficient MOD function.
  763.  
  764.        Suppose that the hash of a given symbol is 13 and the proper slot must
  765.        be located for a hash table of four entries.  If we let "h" be the raw
  766.        result of 13, then our final hash is (h SHL 1) AND ((4-1) SHL 1) or   
  767.  
  768.                                 (13 SHL 1) AND 6 = 2 !
  769.  
  770.        One final  note on this subject.  Given these properties, "Folding" of
  771.        sparse hash  tables is  a rather  trivial exercise  so long as the new
  772.        hash table also contains a number of slots that is a power of 2.  This
  773.        point is  intriguing when  one recalls  that the SYSTEM.TPU hash table
  774.        has only 16 slots rather than the usual 64.
  775.  
  776.  
  777.  
  778.  
  779.  
  780.  
  781.  
  782.  
  783.  
  784.  
  785.  
  786.  
  787.        ----------------------------------------------------------------------
  788.        Rev: August 11, 1990                                           Page 11
  789.  
  790.  
  791.  
  792.                            Inside TURBO Pascal 5.5 Units           
  793.        ----------------------------------------------------------------------
  794.  
  795.        4.4.1.2 SCOPE
  796.  
  797.  
  798.        The INTERFACE  and DEBUG  dictionary hash  tables are  Global in Scope
  799.        even though the symbols accessed directly via the DEBUG hash table may
  800.        be private.   On the other hand, other hash tables are purely local in
  801.        scope.   For example,  the fields declared within a record are reached
  802.        via a  small local  hash  table,  as  are  the  parameters  and  local
  803.        variables declared  within procedures and functions.  Even OBJECTS use
  804.        this technique to provide access to Methods and Object Fields.
  805.  
  806.        Access to  such local  scope fields/methods  requires use of qualified
  807.        names which  ensures conformity  to Pascal scope rules.  The method is
  808.        truly simple and elegant.
  809.  
  810.  
  811.  
  812.        4.4.1.3 SPECIAL CASES
  813.  
  814.  
  815.        The SYSTEM.TPU  Unit is  a special case.  Its INTERFACE and DEBUG hash
  816.        tables have  apparently  been  "hand-tuned"  for  small  size.    Each
  817.        contains only sixteen (16) entries.  In addition, the DEBUG hash table
  818.        is empty  since there  is no  local symbol  generation in  this  unit.
  819.        Therefore, the  DEBUG hash  table does not exist as a separate entity,
  820.        its function being served by the INTERFACE hash table.  The pointer to
  821.        the DEBUG  hash table  (in the  Unit Header) has the same value as the
  822.        pointer to the INTERFACE hash table (SYSTEM unit ONLY).
  823.  
  824.  
  825.  
  826.  
  827.  
  828.  
  829.  
  830.  
  831.  
  832.  
  833.  
  834.  
  835.  
  836.  
  837.  
  838.  
  839.  
  840.  
  841.  
  842.  
  843.  
  844.  
  845.  
  846.  
  847.  
  848.  
  849.  
  850.  
  851.        ----------------------------------------------------------------------
  852.        Rev: August 11, 1990                                           Page 12
  853.  
  854.  
  855.  
  856.                            Inside TURBO Pascal 5.5 Units           
  857.        ----------------------------------------------------------------------
  858.  
  859.        4.4.2 DICTIONARY HEADERS
  860.  
  861.  
  862.        This is  the structure  that anchors  all  information  known  by  the
  863.        compiler about any symbol.  The format is as follows:
  864.  
  865.          +00:    An LL  which points  to the  next (previous) symbol in the
  866.                  same scope which had the same hash function value.
  867.  
  868.          +02:    A character  that defines  the category the symbol belongs
  869.                  to and  defines the  format of  the Dictionary  Stub which
  870.                  follows the Dictionary Header.
  871.  
  872.          +03:    A String  (in the  Pascal sense)  of  variable  size  that
  873.                  contains the  text of  the symbol  (in UPPER-CASE  letters
  874.                  only).   The SizeOf  function is  not  defined  for  these
  875.                  strings since they are truncated to match the symbol size.
  876.                  The "value"  of the  SizeOf function  can be determined by
  877.                  adding  1  to  the  first  byte  in  the  string.    Thus,
  878.                  Ord(Symbol[0])+1 is  the expression  that defines the Size
  879.                  of the  symbol string.  Turbo Pascal defines a symbol as a
  880.                  string of  relatively arbitrary size, the most significant
  881.                  63 characters  of which  will be stored in the dictionary.
  882.                  Thus, we  conclude that  the maximum size of such a string
  883.                  is 64 bytes.
  884.  
  885.  
  886.  
  887.        4.4.3 DICTIONARY STUBS
  888.  
  889.  
  890.        Dictionary Stubs immediately follow their respective headers and their
  891.        format is  determined by  the category  character  in  the  Dictionary
  892.        Header.   The function  of the  stub is  to organize  the  information
  893.        appropriate to  the symbol and provide a means of accessing additional
  894.        information such as type descriptors, constant values, parameter lists
  895.        and nested  scopes.   The format  of each  Stub is  presented  in  the
  896.        following sub-sections.
  897.  
  898.  
  899.  
  900.        4.4.3.1 LABEL DECLARATIVES ("O")
  901.  
  902.  
  903.        This Stub consists of a WORD whose function is (as yet) unknown.
  904.  
  905.  
  906.  
  907.  
  908.  
  909.  
  910.  
  911.  
  912.  
  913.  
  914.  
  915.        ----------------------------------------------------------------------
  916.        Rev: August 11, 1990                                           Page 13
  917.  
  918.  
  919.  
  920.                            Inside TURBO Pascal 5.5 Units           
  921.        ----------------------------------------------------------------------
  922.  
  923.        4.4.3.2 UN-TYPED CONSTANTS ("P")
  924.  
  925.  
  926.        This Stub consists of (2) two fields:
  927.  
  928.          +00:    An LG  which points  to  a  Type  Descriptor  (usually  in
  929.                  SYSTEM.TPU).     This  establishes   the  minimum  storage
  930.                  requirement for  the constant.   The  rules vary  with the
  931.                  type, but  the size  of the  constant  data  field  (which
  932.                  follows) is defined using the Type Descriptor(s).
  933.  
  934.          +04:    The value  of the constant.  For ordinal types, this value
  935.                  is stored as a LONGINT (size=4 bytes).  For Floating-Point
  936.                  types, the  size is  implicit in  the type  itself.    For
  937.                  String types,  the size  is determined  from the length of
  938.                  the string  which is  stored in  the initial  byte of  the
  939.                  constant.
  940.  
  941.  
  942.  
  943.        4.4.3.3 NAMED TYPES ("Q")
  944.  
  945.  
  946.        This Stub  consists of  an  LG  (4-bytes)  that  points  to  the  Type
  947.        Descriptor for this symbol.
  948.  
  949.  
  950.  
  951.  
  952.  
  953.  
  954.  
  955.  
  956.  
  957.  
  958.  
  959.  
  960.  
  961.  
  962.  
  963.  
  964.  
  965.  
  966.  
  967.  
  968.  
  969.  
  970.  
  971.  
  972.  
  973.  
  974.  
  975.  
  976.  
  977.  
  978.  
  979.        ----------------------------------------------------------------------
  980.        Rev: August 11, 1990                                           Page 14
  981.  
  982.  
  983.  
  984.                            Inside TURBO Pascal 5.5 Units           
  985.        ----------------------------------------------------------------------
  986.  
  987.        4.4.3.4 VARIABLES, FIELDS, TYPED CONS ("R")
  988.  
  989.  
  990.        This Stub contains information required to allocate and describe these
  991.        types of entities.  The format and content is as follows:
  992.  
  993.          +00:    A one-byte flag that precisely identifies the class of the
  994.                  item being  described.   The known values and their proper
  995.                  interpretation is as follows:
  996.  
  997.                  0 -> Global Variables Allocated in DS;  
  998.                  1 -> Typed Constants Allocated in DS;   
  999.                  2 -> LOCAL Variables & VALUE Parameters on STACK; 
  1000.                  6 -> ADDRESS Parameters allocated on STACK;  
  1001.                  8 -> Fields suballocated in RECORDS and OBJECTS, plus  
  1002.                       METHODS declared for OBJECTS.
  1003.  
  1004.          +01:    A WORD containing the allocation offset in bytes;
  1005.  
  1006.          +03:    A WORD  whose content  depends on  the one-byte  flag that
  1007.                  this stub  begins  with.    The  context-dependent  values
  1008.                  observed thus far are:
  1009.  
  1010.                  If the  flag is  0, 2  or 6,  then this word is an LL that
  1011.                  locates the containing scope or zero if none;
  1012.  
  1013.                  If the flag is 8, then this word is an LL that locates the
  1014.                  Dictionary Header  for the  next field  or method  defined
  1015.                  within the Record or Object;
  1016.  
  1017.                  If the  flag is  1, then this word is an offset within the
  1018.                  CONST DSeg Map that locates the text of the Typed Constant
  1019.                  Data.
  1020.  
  1021.          +05:    An LG  that locates  the proper  Type Descriptor  for this
  1022.                  symbol.
  1023.  
  1024.  
  1025.  
  1026.  
  1027.  
  1028.  
  1029.  
  1030.  
  1031.  
  1032.  
  1033.  
  1034.  
  1035.  
  1036.  
  1037.  
  1038.  
  1039.  
  1040.  
  1041.  
  1042.  
  1043.        ----------------------------------------------------------------------
  1044.        Rev: August 11, 1990                                           Page 15
  1045.  
  1046.  
  1047.  
  1048.                            Inside TURBO Pascal 5.5 Units           
  1049.        ----------------------------------------------------------------------
  1050.  
  1051.        4.4.3.5 SUBPROGRAMS & METHODS ("S")
  1052.  
  1053.  
  1054.        Subprograms, especially  since Object  Methods are  supported, have  a
  1055.        rather involved stub.  Its format is as follows:
  1056.  
  1057.          +00:    A byte  that contains  bit-switches.   These bit  switches
  1058.                  have a  great deal  to do  with the  size of this stub and
  1059.                  with the  proper interpretation  of  what  follows.    The
  1060.                  observed values of the bit-switches are as follows:
  1061.  
  1062.                  xxxxxxx1 -> Symbol declared in INTERFACE;    
  1063.                  xxxxxx1x -> Symbol is an INLINE Declarative; 
  1064.                  xxxx1x0x -> Symbol has EXTERNAL attribute;   
  1065.                  x001xxxx -> Symbol is an ordinary Object Method;  
  1066.                  x011xxxx -> Symbol is a CONSTRUCTOR Method;  
  1067.                  x101xxxx -> Symbol is a DESTRUCTOR Method;
  1068.  
  1069.          +01:    A Word  whose interpretation depends on whether we have an
  1070.                  INLINE Declarative  Subprogram or  not.   If  this  is  an
  1071.                  INLINE Declarative Subprogram, then this word contains the
  1072.                  byte-count of  the INLINE  code text  at the  end of  this
  1073.                  stub.   Otherwise, this word is the offset within the PROC
  1074.                  Map that locates the object code for this Subprogram.
  1075.  
  1076.          +03:    A Word  that contains  an LL  which locates the containing
  1077.                  scope in the dictionary, or zero if none.
  1078.  
  1079.          +05:    A Word  that contains  an LL  which locates the local Hash
  1080.                  Table for  this scope.  A local hash table provides access
  1081.                  to all  formal parameters of the Subprogram as well as all
  1082.                  Symbols whose  declarations are local to the scope of this
  1083.                  Subprogram.
  1084.  
  1085.          +07:    A Word that is zero unless the symbol is a Virtual Method.
  1086.                  In this  case, then  the content  is the offset within the
  1087.                  VMT for  the owning  object that  defines  where  the  FAR
  1088.                  POINTER to this Virtual Method is stored.
  1089.  
  1090.          +09:    A Word  that is  zero unless  the symbol  is a Method.  In
  1091.                  this case,  then the  content is  an LL  which locates the
  1092.                  next METHOD for this Object.
  1093.  
  1094.          +0B:    A complete  Type-Descriptor  for  this  Subprogram.    The
  1095.                  length is  variable and  depends upon the number of Formal
  1096.                  Parameters declared in the header.  A complete description
  1097.                  of  this   subfield  is   found   in   a   later   section
  1098.                  (4.4.4.3.2.6).
  1099.  
  1100.          +??:    If  this   Symbol   represents   an   INLINE   Declarative
  1101.                  Subprogram, then  the object-code  text begins  here.  The
  1102.                  byte-count of  the text  occurs at  offset 0001h  in  this
  1103.                  stub.
  1104.  
  1105.  
  1106.  
  1107.        ----------------------------------------------------------------------
  1108.        Rev: August 11, 1990                                           Page 16
  1109.  
  1110.  
  1111.  
  1112.                            Inside TURBO Pascal 5.5 Units           
  1113.        ----------------------------------------------------------------------
  1114.  
  1115.        4.4.3.6 TURBO STD PROCEDURES ("T")
  1116.  
  1117.  
  1118.        This Stub consists of two bytes, the first of which is unique for each                                                                              |
  1119.        procedure and  increments by  4.   I have  found nothing in the SYSTEM                                                                              |
  1120.        unit (which  is where  this entry  appears) that  this seems  directly                                                                              |
  1121.        related to.  The second byte is always zero.                                                                              |
  1122.  
  1123.  
  1124.  
  1125.        4.4.3.7 TURBO STD FUNCTIONS ("U")
  1126.  
  1127.  
  1128.        This Stub consists of two bytes, the first of which is unique for each                                                                              |
  1129.        function and increments by 4.  I have found nothing in the SYSTEM unit                                                                              |
  1130.        (which is  where this  entry appears) that this seems directly related                                                                              |
  1131.        to.   I wouldn't  be surprised if this byte were an index into a TURBO                                                                              |
  1132.        compiler table that points to specialized parse tables/action routines                                                                              |
  1133.        for handling these functions and their non-standard parameter lists.                                                                              |
  1134.  
  1135.        The second byte seems to be a flag having the values $00, $40 and $C0.                                                                              |
  1136.        I strongly  suspect that  the flag  $C0 marks  exactly those functions                                                                              |
  1137.        which may  be evaluated at compile-time.  The meaning behind the other                                                                              |
  1138.        values is not known to me.                                                                              |
  1139.  
  1140.  
  1141.  
  1142.        4.4.3.8 TURBO STD "NEW" ROUTINE ("V")
  1143.  
  1144.  
  1145.        This Stub consists of a WORD whose function is (as yet) unknown.  This                                                                              |
  1146.        is the  only Standard  Turbo routine that can behave as a procedure as                                                                              |
  1147.        well as a function (returning a pointer value).                                                                              |
  1148.  
  1149.  
  1150.  
  1151.        4.4.3.9 TURBO STD PORT ARRAYS ("W")
  1152.  
  1153.  
  1154.        This Stub  consists of  a byte whose value is 0 for byte arrays, and 1
  1155.        for word arrays.
  1156.  
  1157.  
  1158.  
  1159.        4.4.3.10 TURBO STD EXTERNAL VARIABLES ("X")
  1160.  
  1161.  
  1162.        This Stub  consists of  an  LG  (4-bytes)  that  points  to  the  Type
  1163.        Descriptor for this symbol.
  1164.  
  1165.  
  1166.  
  1167.  
  1168.  
  1169.  
  1170.  
  1171.        ----------------------------------------------------------------------
  1172.        Rev: August 11, 1990                                           Page 17
  1173.  
  1174.  
  1175.  
  1176.                            Inside TURBO Pascal 5.5 Units           
  1177.        ----------------------------------------------------------------------
  1178.  
  1179.        4.4.3.11 UNITS ("Y")
  1180.  
  1181.  
  1182.        Unit Stubs have the following content:
  1183.  
  1184.          +00:    A Word  whose apparently  reserved for use by the Compiler
  1185.                  or Linker.
  1186.  
  1187.          +02:    A Word that seems to contain some kind of "signature" used
  1188.                  to  detect   inconsistent  Unit  Versions.    This  author
  1189.                  suspects that  this consists  of some kind of sum-check or
  1190.                  hash total  but has not yet identified the algorithm which
  1191.                  computes the value stored in this word.
  1192.  
  1193.          +04:    A Word  that contains  an LL  which locates  the Successor
  1194.                  Unit in  the "Uses"  list.   In fact,  the "Uses" lists of
  1195.                  both the INTERFACE and IMPLEMENTATION sections of the Unit
  1196.                  are merged  by this  Word into  a single list.  A value of
  1197.                  zero is used to indicate no successor.
  1198.  
  1199.          +06:    A Word  that contains  an LL which locates the Predecessor
  1200.                  Unit in  the "Uses" list.  For the SYSTEM unit entry, this
  1201.                  value is  always zero to indicate no predecessor.  For the
  1202.                  Unit being compiled, this LL locates the final Unit in the
  1203.                  combined "Uses" list.
  1204.  
  1205.        In effect,  the two  LL's at  offsets 0004 and 0006 organize the units
  1206.        into both  forward and backward linked chains.  The entry for the unit
  1207.        being compiled  is effectively  the head  of both  the forward and the
  1208.        backward chains.  The final unit in the merged "Uses" list is the tail
  1209.        of the  forward chain, and the SYSTEM unit is the tail of the backward
  1210.        chain.
  1211.  
  1212.  
  1213.  
  1214.  
  1215.  
  1216.  
  1217.  
  1218.  
  1219.  
  1220.  
  1221.  
  1222.  
  1223.  
  1224.  
  1225.  
  1226.  
  1227.  
  1228.  
  1229.  
  1230.  
  1231.  
  1232.  
  1233.  
  1234.  
  1235.        ----------------------------------------------------------------------
  1236.        Rev: August 11, 1990                                           Page 18
  1237.  
  1238.  
  1239.  
  1240.                            Inside TURBO Pascal 5.5 Units           
  1241.        ----------------------------------------------------------------------
  1242.  
  1243.        4.4.4 TYPE DESCRIPTORS
  1244.  
  1245.  
  1246.        Type Descriptors  store much  of the semantic information that applies
  1247.        to the  symbols declared  in the  unit.  Implementation details can be
  1248.        managed using  high-level abstractions  and these  abstractions can be
  1249.        shared.
  1250.  
  1251.  
  1252.  
  1253.        4.4.4.1 SCOPE
  1254.  
  1255.  
  1256.        Type Descriptor  sharing can  occur across  the boundaries  which  are
  1257.        implicit in  unit modules.   Thus,  a type  defined in one unit may be
  1258.        "imported" by  some other  module.  Also, the pre-defined Pascal Types
  1259.        (plus the  Turbo Pascal extensions) are defined in the SYSTEM.TPU unit
  1260.        and there  needs to  be a  means of  "importing" such Type Descriptors
  1261.        during compilation.  This is precisely the objective of the LG locator
  1262.        which was  described in  section 2.2  (above).   Type Descriptors  are
  1263.        NEVER copied between units.  The binding always occurs by reference at
  1264.        compile time  and this helps support the technique of modifying a unit
  1265.        and compiling  it to a .TPU file, then re-compiling all units/programs
  1266.        that "USE" it.
  1267.  
  1268.        Type Descriptors  have many  roles so  their format  varies.   We have
  1269.        divided these  structures into  two parts:   The PREFIX Part (which is
  1270.        always present  and) whose  format is  fairly constant  and the SUFFIX
  1271.        Part whose  content and format depends on the attributes that are part
  1272.        of the type definition.
  1273.  
  1274.  
  1275.  
  1276.  
  1277.  
  1278.  
  1279.  
  1280.  
  1281.  
  1282.  
  1283.  
  1284.  
  1285.  
  1286.  
  1287.  
  1288.  
  1289.  
  1290.  
  1291.  
  1292.  
  1293.  
  1294.  
  1295.  
  1296.  
  1297.  
  1298.  
  1299.        ----------------------------------------------------------------------
  1300.        Rev: August 11, 1990                                           Page 19
  1301.  
  1302.  
  1303.  
  1304.                            Inside TURBO Pascal 5.5 Units           
  1305.        ----------------------------------------------------------------------
  1306.  
  1307.        4.4.4.2 PREFIX PART
  1308.  
  1309.  
  1310.        The Prefix  Part of  every Type Descriptor consists of four (4) bytes.
  1311.        The usage  is consistent for all types observed by this author and the
  1312.        format is as follows:
  1313.  
  1314.          +00:    A Byte  that identifies  the format  of the  Suffix  part.
  1315.                  This is essentially based on several high-level categories
  1316.                  which the Suffix Parts support directly.  The observed set
  1317.                  of values is as follows:
  1318.  
  1319.                  00h -> an un-typed entity;    
  1320.                  01h -> an ARRAY type;    
  1321.                  02h -> a RECORD type;    
  1322.                  03h -> an OBJECT type;   
  1323.                  04h -> a FILE type (other than TEXT);   
  1324.                  05h -> a TEXT File type; 
  1325.                  06h -> a SUBPROGRAM type;     
  1326.                  07h -> a SET type;  
  1327.                  08h -> a POINTER type;   
  1328.                  09h -> a STRING type;    
  1329.                  0Ah -> an 8087 Floating-Point type;     
  1330.                  0Bh -> a REAL type; 
  1331.                  0Ch -> a Fixed-Point ordinal type; 
  1332.                  0Dh -> a BOOLEAN type;   
  1333.                  0Eh -> a CHAR type; 
  1334.                  0Fh -> an Enumerated ordinal type.
  1335.  
  1336.          +01:    A Byte  used as a modifier.  Since the above scheme is too
  1337.                  general for  machine-dependent  details  such  as  storage
  1338.                  width  and  sign  control,  this  modifier  byte  supplies
  1339.                  additional data  as required.   The  author has identified
  1340.                  several cases  in which  this information is vital but has
  1341.                  not spent  very much time on the subject.  The chief areas
  1342.                  of importance seem to be in the 8087 Floating-Point types,
  1343.                  and the  Fixed-Point ordinal types.  The semantics seem to
  1344.                  be as follows:
  1345.  
  1346.                  0A 00 -> The type "SINGLE"    
  1347.                  0A 02 -> The type "EXTENDED"  
  1348.                  0A 04 -> The type "DOUBLE"    
  1349.                  0A 06 -> The type "COMP" 
  1350.  
  1351.                  0C 00 -> an un-named BYTE integer  
  1352.                  0C 01 -> The type "SHORTINT"  
  1353.                  0C 02 -> The type "BYTE" 
  1354.                  0C 04 -> an un-named WORD integer  
  1355.                  0C 05 -> The type "INTEGER"   
  1356.                  0C 06 -> The type "WORD" 
  1357.                  0C 0C -> an un-named double-word integer     
  1358.                  0C 0D -> The type "LONGINT"
  1359.  
  1360.  
  1361.  
  1362.  
  1363.        ----------------------------------------------------------------------
  1364.        Rev: August 11, 1990                                           Page 20
  1365.  
  1366.  
  1367.  
  1368.                            Inside TURBO Pascal 5.5 Units           
  1369.        ----------------------------------------------------------------------
  1370.  
  1371.                  One important  feature of  the above semantics is the fact
  1372.                  that an un-typed CONST declaration refers to the above two
  1373.                  bytes  to  determine  the  storage  space  needed  in  the
  1374.                  dictionary for  the data  value of the constant.  This can
  1375.                  be a  little involved  however as the constant may contain
  1376.                  its own  length descriptor  (as in the case of a character
  1377.                  string)   in which  case it  may be sufficient to identify
  1378.                  the high-level type category without any modifier byte.
  1379.  
  1380.          +02:    A Word  that contains  the number of bytes of storage that
  1381.                  are required  to contain  an object/entity  of this  type.
  1382.                  For types  that represent variable-length objects/entities
  1383.                  such as  strings, this  word may define the value returned
  1384.                  by the SIZEOF function as applied to the type.
  1385.  
  1386.  
  1387.  
  1388.        4.4.4.3 SUFFIX PARTS
  1389.  
  1390.  
  1391.        Suffix Parts further refine the implementation details of the type and
  1392.        also provide  subrange constraints  where appropriate.   In some cases
  1393.        the Suffix  part is  empty since  all semantic  data for  the type  is
  1394.        contained in the Prefix part.
  1395.  
  1396.  
  1397.  
  1398.        4.4.4.3.1 UN-TYPED
  1399.  
  1400.  
  1401.        This Suffix Part is empty.  Nothing is known about an un-typed entity.
  1402.  
  1403.  
  1404.  
  1405.  
  1406.  
  1407.  
  1408.  
  1409.  
  1410.  
  1411.  
  1412.  
  1413.  
  1414.  
  1415.  
  1416.  
  1417.  
  1418.  
  1419.  
  1420.  
  1421.  
  1422.  
  1423.  
  1424.  
  1425.  
  1426.  
  1427.        ----------------------------------------------------------------------
  1428.        Rev: August 11, 1990                                           Page 21
  1429.  
  1430.  
  1431.  
  1432.                            Inside TURBO Pascal 5.5 Units           
  1433.        ----------------------------------------------------------------------
  1434.  
  1435.        4.4.4.3.2 STRUCTURED TYPES
  1436.  
  1437.  
  1438.        The structured  types represent  aggregates of  lower-level types.  We
  1439.        include ARRAY,  RECORD, OBJECT,  FILE, TEXT,  SET, POINTER  and STRING
  1440.        types in this category.
  1441.  
  1442.  
  1443.  
  1444.        4.4.4.3.2.1 ARRAY TYPES
  1445.  
  1446.  
  1447.        The Suffix  Part of  the ARRAY type is so constructed as to be able to
  1448.        support recursive  or nested  definition of arrays.  The suffix format
  1449.        is as follows:
  1450.  
  1451.          +00:    An LG that locates the Type Descriptor for the "base-type"
  1452.                  of the  array.   This is  the type  of  the  entity  being
  1453.                  arrayed and may itself be an array.
  1454.  
  1455.          +04:    An LG  that locates  the Type  Descriptor  for  the  array
  1456.                  bounds which is a constrained ordinal type or subrange.
  1457.  
  1458.  
  1459.  
  1460.        4.4.4.3.2.2 RECORD TYPES
  1461.  
  1462.  
  1463.        RECORD types  have nested  scopes.   The Suffix  part provides  a base
  1464.        structure by  which to  locate the  fields local  to the  scope of the
  1465.        Record type itself.  The format is as follows:
  1466.  
  1467.          +00:    A Word containing an LL which locates the local Hash Table
  1468.                  that provides access to the fields in the nested scope.
  1469.  
  1470.          +02:    A Word  containing an  LL  which  locates  the  Dictionary
  1471.                  Header of  the initial  field in  the nested  scope.  This
  1472.                  supports a  "left-to-right" traversal  of the  fields in a
  1473.                  record.
  1474.  
  1475.  
  1476.  
  1477.  
  1478.  
  1479.  
  1480.  
  1481.  
  1482.  
  1483.  
  1484.  
  1485.  
  1486.  
  1487.  
  1488.  
  1489.  
  1490.  
  1491.        ----------------------------------------------------------------------
  1492.        Rev: August 11, 1990                                           Page 22
  1493.  
  1494.  
  1495.  
  1496.                            Inside TURBO Pascal 5.5 Units           
  1497.        ----------------------------------------------------------------------
  1498.  
  1499.        4.4.4.3.2.3 OBJECT TYPES
  1500.  
  1501.  
  1502.        OBJECT types also have nested scopes.  The Suffix part provides a base
  1503.        structure by which to locate the fields and METHODS local to the scope
  1504.        of  the  OBJECT  type  itself.    In  addition,  inheritance  and  VMT
  1505.        particulars are stored.  The format is as follows:
  1506.  
  1507.          +00:    A Word containing an LL which locates the local Hash Table
  1508.                  that provides  access to  the fields  and METHODS local to
  1509.                  the nested scope.
  1510.  
  1511.          +02:    A Word  containing an  LL  which  locates  the  Dictionary
  1512.                  Header of the initial field or METHOD in the nested scope.
  1513.                  This supports  a "left-to-right"  traversal of  the fields
  1514.                  and METHODS in an OBJECT.
  1515.  
  1516.          +04:    An LG  which locates  the Type  Descriptor of  the  Parent
  1517.                  Object.  This field is zero if there is no such Parent.
  1518.  
  1519.          +08:    A Word  which contains  the size  in bytes  of the VMT for
  1520.                  this Object.   This field is zero if the object employs no
  1521.                  Virtual Methods.
  1522.  
  1523.          +0A:    A Word which contains the offset within the CONST DSeg Map
  1524.                  that locates  the VMT  skeleton or template segment.  This
  1525.                  field equals  FFFFh  if  the  object  employs  no  Virtual
  1526.                  Methods.
  1527.  
  1528.          +0C:    A Word which contains the offset within an Object instance
  1529.                  where the NEAR POINTER to the VMT for the object is stored
  1530.                  (within the DATA SEGMENT).  This field equals FFFFh if the
  1531.                  object employs no Virtual Methods.
  1532.  
  1533.          +0E:    A Word  which contains  an LL which locates the Dictionary
  1534.                  Header for the name of the OBJECT itself.
  1535.  
  1536.  
  1537.  
  1538.        4.4.4.3.2.4 FILE (NON-TEXT) TYPES
  1539.  
  1540.  
  1541.        This Suffix  consists of an LG that locates the Type Descriptor of the
  1542.        base type  of the  file.  Note that the Type Descriptor may be that of
  1543.        an un-typed entity (for un-typed files).
  1544.  
  1545.  
  1546.  
  1547.        4.4.4.3.2.5 TEXT FILE TYPES
  1548.  
  1549.  
  1550.        This Suffix  consists of an LG that locates the Type Descriptor of the
  1551.        base type of the file -- in this case SYSTEM.CHAR.
  1552.  
  1553.  
  1554.  
  1555.        ----------------------------------------------------------------------
  1556.        Rev: August 11, 1990                                           Page 23
  1557.  
  1558.  
  1559.  
  1560.                            Inside TURBO Pascal 5.5 Units           
  1561.        ----------------------------------------------------------------------
  1562.  
  1563.        4.4.4.3.2.6 SET TYPES
  1564.  
  1565.  
  1566.        This Suffix  consists of  an LG  that locates the base-type of the set
  1567.        itself.    Pascal  limits  such  entities  to  simple  ordinals  whose
  1568.        cardinality is limited to 256.
  1569.  
  1570.  
  1571.  
  1572.        4.4.4.3.2.7 POINTER TYPES
  1573.  
  1574.  
  1575.        This Suffix consists of an LG that locates the base-type of the entity
  1576.        pointed at.
  1577.  
  1578.  
  1579.  
  1580.        4.4.4.3.2.8 STRING TYPES
  1581.  
  1582.  
  1583.        This is a special case of an ARRAY type.  The format is as follows:
  1584.  
  1585.          +00:    An LG to the Type Descriptor SYSTEM.CHAR which is the base
  1586.                  type of all Turbo Pascal Strings.
  1587.  
  1588.          +04:    An  LG  to  the  Type  Descriptor  for  the  array  bounds
  1589.                  constraints for the string.
  1590.  
  1591.  
  1592.  
  1593.        4.4.4.3.3 FLOATING-POINT TYPES
  1594.  
  1595.  
  1596.        The Suffix  part for  all Floating-Point  types is  EMPTY.   All  data
  1597.        needed to  specify these  approximate number types is contained in the
  1598.        Prefix part.   The  Types included  in this  class are SINGLE, DOUBLE,
  1599.        EXTENDED, COMP and REAL.
  1600.  
  1601.  
  1602.  
  1603.        4.4.4.3.4 ORDINAL TYPES
  1604.  
  1605.  
  1606.        The Ordinal  Types consist  of the  various "integer"  types plus  the
  1607.        BOOLEAN, CHAR and Enumerated types.
  1608.  
  1609.  
  1610.  
  1611.  
  1612.  
  1613.  
  1614.  
  1615.  
  1616.  
  1617.  
  1618.  
  1619.        ----------------------------------------------------------------------
  1620.        Rev: August 11, 1990                                           Page 24
  1621.  
  1622.  
  1623.  
  1624.                            Inside TURBO Pascal 5.5 Units           
  1625.        ----------------------------------------------------------------------
  1626.  
  1627.        4.4.4.3.4.1 "INTEGERS"
  1628.  
  1629.  
  1630.        These types  include BYTE, SMALLINT, WORD, INTEGER and LONGINT.  Their
  1631.        Suffix parts are identical in format:
  1632.  
  1633.          +00:    A double-word  containing the  LOWER bound of the subrange
  1634.                  constraint on the type;
  1635.  
  1636.          +04:    A double-word  containing the  UPPER bound of the subrange
  1637.                  constraint on the type;
  1638.  
  1639.          +08:    An LG  that locates  the Type  Descriptor of  the  largest
  1640.                  upward compatible  type.  This is the Type Descriptor that
  1641.                  is used  to control  the width  of an un-typed constant in
  1642.                  the dictionary  stub.  For the "integer" types, this is an
  1643.                  LG to SYSTEM.LONGINT.
  1644.  
  1645.  
  1646.  
  1647.        4.4.4.3.4.2 BOOLEANS
  1648.  
  1649.  
  1650.        This type Suffix has the following format:
  1651.  
  1652.          +00:    A double-word  containing the  LOWER bound of the subrange
  1653.                  constraint on the type;
  1654.  
  1655.          +04:    A double-word  containing the  UPPER bound of the subrange
  1656.                  constraint on the type;
  1657.  
  1658.          +08:    An LG  that locates  the Type  Descriptor  SYSTEM.BOOLEAN.
  1659.                  There is no "upward compatible" type.
  1660.  
  1661.  
  1662.  
  1663.        4.4.4.3.4.3 CHARS
  1664.  
  1665.  
  1666.        This type Suffix has the following format:
  1667.  
  1668.          +00:    A double-word  containing the  LOWER bound of the subrange
  1669.                  constraint on the type;
  1670.  
  1671.          +04:    A double-word  containing the  UPPER bound of the subrange
  1672.                  constraint on the type;
  1673.  
  1674.          +08:    An LG that locates the Type Descriptor SYSTEM.CHAR.  There
  1675.                  is no "upward compatible" type.
  1676.  
  1677.  
  1678.  
  1679.  
  1680.  
  1681.  
  1682.  
  1683.        ----------------------------------------------------------------------
  1684.        Rev: August 11, 1990                                           Page 25
  1685.  
  1686.  
  1687.  
  1688.                            Inside TURBO Pascal 5.5 Units           
  1689.        ----------------------------------------------------------------------
  1690.  
  1691.        4.4.4.3.4.4 ENUMERATIONS
  1692.  
  1693.  
  1694.        This type Suffix is unusual and has the following format:
  1695.  
  1696.          +00:    A double-word  containing the  LOWER bound of the subrange
  1697.                  constraint on the type;
  1698.  
  1699.          +04:    A double-word  containing the  UPPER bound of the subrange
  1700.                  constraint on the type;
  1701.  
  1702.          +08:    An  LG  that  locates  the  Prefix  of  the  current  Type
  1703.                  Descriptor.  There is no upward compatible type.
  1704.  
  1705.        What follows  is a full-fledged SET Type Descriptor whose base type is
  1706.        the Type Descriptor of the Enumerated Type itself.  The author has not
  1707.        yet discovered the reason for this.
  1708.  
  1709.  
  1710.  
  1711.        4.4.4.3.5 SUBPROGRAM TYPES
  1712.  
  1713.  
  1714.        The length of this Suffix is variable.  The format is as follows:
  1715.  
  1716.          +00:    An LG  that locates  the Type  Descriptor of  the FUNCTION
  1717.                  result returned  by the Subprogram.  This field is zero if
  1718.                  the Subprogram is a PROCEDURE.
  1719.  
  1720.          +04:    A Word  that contains  the number  of Formal Parameters in
  1721.                  the Function/Procedure  header.   If non-zero,  then  this
  1722.                  word is  followed by the parameter list itself as a simple
  1723.                  array of parameter descriptors.
  1724.  
  1725.                  The format of a parameter descriptor is as follows:
  1726.  
  1727.                  0000: An LG that locates the Type Descriptor of the    
  1728.                        corresponding parameter;
  1729.  
  1730.                  0004: A Byte that identifies the parameter passing     
  1731.                        mechanism used for this entry as follows:
  1732.  
  1733.                        02h -> VALUE of parameter is passed on STACK,    
  1734.                        06h -> ADDRESS of parameter is passed on STACK.
  1735.  
  1736.  
  1737.  
  1738.  
  1739.  
  1740.  
  1741.  
  1742.  
  1743.  
  1744.  
  1745.  
  1746.  
  1747.        ----------------------------------------------------------------------
  1748.        Rev: August 11, 1990                                           Page 26
  1749.  
  1750.  
  1751.  
  1752.                            Inside TURBO Pascal 5.5 Units           
  1753.        ----------------------------------------------------------------------
  1754.  
  1755.        5. MAPS AND LISTS
  1756.  
  1757.  
  1758.        The "MAPS  and LISTS"  are not part of the symbol dictionary.  Rather,
  1759.        these structures provide access to the Code and Data Segments produced
  1760.        by the  compiler or  included via  the {$L  name.OBJ} directive.   The
  1761.        format and  purpose (as  understood by  this author)  of each of these
  1762.        tables is explained in the following sections.
  1763.  
  1764.  
  1765.  
  1766.        5.1 PROC MAP
  1767.  
  1768.  
  1769.        The PROC  Map provides a means of associating the various Function and
  1770.        Procedure declarations with the Code Segments.  There is some evidence
  1771.        that the  Compiler produces  CODE (and  DATA) Segments for EACH of the
  1772.        Subprograms defined  in the  Unit as  well as  for the  un-named  Unit
  1773.        Initialization code block.  There is also evidence that EXTERNAL PROCs                                                                              |
  1774.        must be  assembled separately  in order  to exploit  fully  the  Turbo
  1775.        "Smart Linker" since Turbo Pascal places some significant restrictions
  1776.        on  EXTERNAL  routines  in  the  area  of  Segment  Names  and  Types.
  1777.        Specifically, only  code segments named "CODE" and data segments named
  1778.        "DATA" will  be used by the "Smart Linker" as sources of code and data
  1779.        for inclusion in a Turbo Pascal .EXE file.
  1780.  
  1781.        The first  entry in  the PROC  Map is reserved for Unit Initialization
  1782.        block.   If there  is no Unit Initialization block, this entry will be                                                                              |
  1783.        filled with  $FF.  In addition, each and every PROC in the Unit has an                                                                              |
  1784.        entry in this table.
  1785.  
  1786.        If an  EXTERNAL routine  is included, then ALL PUBLIC PROC definitions
  1787.        in that  routine must  be declared  in the  Unit Source  Code with the
  1788.        EXTERNAL attribute.
  1789.  
  1790.        The size  of the  PROC Map  Table (in  Bytes) is  implied in  the Unit
  1791.        Header by the LL's that occur at offsets +0C and +0E.
  1792.  
  1793.        The Format of a single PROC Map Entry is as follows:
  1794.  
  1795.          +00:    A Word  that contains an offset within the CSeg Map.  This
  1796.                  is used to locate the code segment containing the PROC.
  1797.  
  1798.          +02:    A Word  that contains  an offset  within the  CODE Segment
  1799.                  that defines  the PROC  entry point  relative to  the load
  1800.                  point of the referenced CODE Segment.
  1801.  
  1802.  
  1803.  
  1804.  
  1805.  
  1806.  
  1807.  
  1808.  
  1809.  
  1810.  
  1811.        ----------------------------------------------------------------------
  1812.        Rev: August 11, 1990                                           Page 27
  1813.  
  1814.  
  1815.  
  1816.                            Inside TURBO Pascal 5.5 Units           
  1817.        ----------------------------------------------------------------------
  1818.  
  1819.        5.2 CSEG MAP
  1820.  
  1821.  
  1822.        The CSeg  Map provides  a convenient  descriptor table  for each  CODE
  1823.        Segment present  in the  Unit and serves to relate these segments with
  1824.        the Segment  Relocation Data  and the  Segment Trace  Table.  It seems
  1825.        reasonable to infer that the "Smart Linker" is able to include/exclude
  1826.        code/data at the SEGMENT level only.
  1827.  
  1828.        The CSeg  Map is  an array  of fixed-length records whose format is as
  1829.        follows:
  1830.  
  1831.          +00:    A Word apparently reserved for use by TURBO.
  1832.  
  1833.          +02:    A Word that contains the Segment Length (in bytes).
  1834.  
  1835.          +04:    A Word  that contains  the Length  of the  Relocation Data
  1836.                  Table for this Code Segment (in bytes).
  1837.  
  1838.          +06:    A Word  that contains  the offset of the Trace Table Entry
  1839.                  for this  Segment (if it was compiled with DEBUG Support).
  1840.                  If there  is no  Trace Table  for this  segment, then this
  1841.                  Word contains FFFFh.
  1842.  
  1843.  
  1844.  
  1845.        5.3 TYPED CONST DSEG MAP
  1846.  
  1847.  
  1848.        The CONST  DSeg Map  provides a  convenient descriptor  table for each
  1849.        DATA Segment  present in the Unit which was spawned by the presence of
  1850.        Typed Constants  or VMT's  in the  Pascal Code.   It  serves to relate
  1851.        these segments  with the  Segment Relocation  Data and  with the  Code
  1852.        Segments that refer to these DATA elements.
  1853.  
  1854.        The CONST DSeg Map is an array of fixed-length records whose format is
  1855.        as follows:
  1856.  
  1857.          +00:    A Word apparently reserved for use by TURBO.
  1858.  
  1859.          +02:    A Word that contains the Segment Length (in bytes).
  1860.  
  1861.          +04:    A Word  that contains  the Length  of the  Relocation Data
  1862.                  Table for this DATA Segment (in bytes).
  1863.  
  1864.          +06:    A Word  that contains  an LL which locates the OBJECT that
  1865.                  owns this VMT skeleton or zero if the segment is not a VMT
  1866.                  skeleton.
  1867.  
  1868.        It is  possible to determine the containing scope for a Typed Constant
  1869.        declaration but -- unless it is for a VMT -- the job is a bit tedious.
  1870.        Essentially, one has to search the Symbol Dictionary for a declaration
  1871.        whose offset  points to  a given  entry and  the complete path to that
  1872.        symbol must  be recorded.   Our  program doesn't do this but it can be
  1873.        done if the required dictionary entries are present.
  1874.  
  1875.        ----------------------------------------------------------------------
  1876.        Rev: August 11, 1990                                           Page 28
  1877.  
  1878.  
  1879.  
  1880.                            Inside TURBO Pascal 5.5 Units           
  1881.        ----------------------------------------------------------------------
  1882.  
  1883.  
  1884.  
  1885.        5.4 GLOBAL VAR DSEG MAP
  1886.  
  1887.  
  1888.        The VAR  DSeg Map provides a convenient descriptor table for each DATA
  1889.        Segment present in the Unit.
  1890.  
  1891.        One entry  exists for  each CODE  segment which refers to GLOBAL VAR's
  1892.        allocated in  the DATA  Segment.   These references may be seen in the
  1893.        Relocation Data Table.  Each EXTERNAL CSeg having a segment named DATA
  1894.        also spawns  an entry in this table.  Only the Code Segments that meet
  1895.        these criteria cause entries to be generated in the VAR Dseg Map.
  1896.  
  1897.        The VAR  DSeg Map  is an array of fixed-length records whose format is
  1898.        as follows:
  1899.  
  1900.          +00:    A Word apparently reserved for use by TURBO.
  1901.  
  1902.          +02:    A Word  that contains the Segment Length (in bytes).  This
  1903.                  may be zero, especially if the EXTERNAL routine contains a
  1904.                  DATA segment  whose sole purpose is to declare one or more
  1905.                  EXTRN symbols  that  are  defined  in  some  DATA  segment
  1906.                  external to the Assembly.
  1907.  
  1908.          +04:    A Word apparently reserved for use by TURBO.
  1909.  
  1910.          +06:    A Word apparently reserved for use by TURBO.
  1911.  
  1912.        To determine  the identity of the CSeg that owns some particular entry
  1913.        in this  table, examine  the Relocation Data for ALL CSegs.  Each CSeg
  1914.        which makes reference to a DATA segment has an entry in this table.
  1915.  
  1916.  
  1917.  
  1918.        5.5 DONOR UNIT LIST
  1919.  
  1920.  
  1921.        This list contains an entry for each Unit (taken from the "USES" list)
  1922.        which MAY  contribute either CODE or DATA to the executable file.  Not
  1923.        all units do make such a contribution as some exist merely to define a
  1924.        collection of  Types, etc.  A Unit gets into this list if there exists
  1925.        a single  Relocation Data  Entry that  references CODE or DATA in that
  1926.        Unit.
  1927.  
  1928.        The list  is comprised  of elements  whose SIZE  is variable and whose
  1929.        format is as follows:
  1930.  
  1931.          +00:    A WORD apparently reserved for use by TURBO.
  1932.  
  1933.          +02:    A variable-length String containing the unit name.
  1934.  
  1935.  
  1936.  
  1937.  
  1938.  
  1939.        ----------------------------------------------------------------------
  1940.        Rev: August 11, 1990                                           Page 29
  1941.  
  1942.  
  1943.  
  1944.                            Inside TURBO Pascal 5.5 Units           
  1945.        ----------------------------------------------------------------------
  1946.  
  1947.        5.6 SOURCE FILE LIST
  1948.  
  1949.  
  1950.        This list contains an entry for each "source" file used to compile the
  1951.        Unit.   This includes the Primary Pascal file, files containing Pascal
  1952.        code included  by means  of the  {$I filename.xxx} compiler directive,
  1953.        and .OBJ files included by the {$L filename.OBJ} compiler directive.
  1954.  
  1955.        The order  of entries  in this list is critical since it maps the CODE
  1956.        segments stored in the unit.  The order of the entries is as follows:
  1957.  
  1958.          1)      The Primary Pascal file;
  1959.  
  1960.          2)      All Included Pascal files;
  1961.  
  1962.          3)      All Included .OBJ files.
  1963.  
  1964.        Mapping of CSegs to files is done as follows:
  1965.  
  1966.          a)      Each .OBJ file contributes a SINGLE Code Segment (if any).
  1967.                  Note that this author has not observed an .OBJ module that
  1968.                  contains only  a DATA  Segment (but  that seems a distinct
  1969.                  possibility).
  1970.  
  1971.          b)      The Primary  Pascal file (augmented by all included Pascal
  1972.                  Files) contributes zero or more CODE Segments.
  1973.  
  1974.        Therefore, there  are at least as many CSeg entries as .OBJ files.  If
  1975.        more, then  the excess entries (those at the front of the list) belong
  1976.        to the Pascal files that make up the Pascal source for the unit.
  1977.  
  1978.        The format of an entry in this list is as follows:
  1979.  
  1980.          +00:    A flag byte that indicates the type of file represented;    
  1981.  
  1982.                  04h -> the Primary Pascal Source File,  
  1983.                  03h -> an Included Pascal Source File,  
  1984.                  05h -> an .OBJ file that contains a CODE segment.
  1985.  
  1986.          +01:    A Word apparently reserved for use by the Compiler/Linker.
  1987.  
  1988.          +03:    A Word  that is zero for .OBJ files and which contains the
  1989.                  file directory time-stamp for Pascal Files.
  1990.  
  1991.          +05:    A Word  that is zero for .OBJ files and which contains the
  1992.                  file directory date-stamp for Pascal Files.
  1993.  
  1994.          +07:    A  variable-sized   string  containing  the  filename  and
  1995.                  extension of the file used during compilation.
  1996.  
  1997.  
  1998.  
  1999.  
  2000.  
  2001.  
  2002.  
  2003.        ----------------------------------------------------------------------
  2004.        Rev: August 11, 1990                                           Page 30
  2005.  
  2006.  
  2007.  
  2008.                            Inside TURBO Pascal 5.5 Units           
  2009.        ----------------------------------------------------------------------
  2010.  
  2011.        5.7 DEBUG TRACE TABLE
  2012.  
  2013.  
  2014.        If Debug  support was  selected at  compile time, then all Pascal code
  2015.        which supports  Debugging produces  an entry in this table.  The table
  2016.        entries themselves are variable in size and have the following format:
  2017.  
  2018.          +00:    A Word  which contains  an LL  that locates  the Directory
  2019.                  Header of the Symbol (a PROC name) this entry represents.
  2020.  
  2021.          +02:    A Word  which contains  the offset (within the Source File
  2022.                  List) of  the entry that names the file that generated the
  2023.                  CSeg being traced.  This allows the file included by means
  2024.                  of the  {$I filename} directive to be identified for DEBUG
  2025.                  purposes, as well as code produced from the Primary File.
  2026.  
  2027.          +04:    A Word containing the number of bytes of data that precede
  2028.                  the BEGIN statement code in the segment.  For Pascal PROCS
  2029.                  these  bytes   consist  of   literal  constants,  un-typed                                                                              |
  2030.                  constants, and  other data  such as range-checking limits,                                                                              |
  2031.                  etc.
  2032.  
  2033.          +06:    A Word  containing the  Line Number of the BEGIN statement
  2034.                  for the PROC.
  2035.  
  2036.          +08:    A Word  containing the  number of  lines of Source Code to
  2037.                  Trace in this Segment.
  2038.  
  2039.          +0A:    An array  of bytes  whose size   is at least the number of
  2040.                  source code  lines in  the PROC.   Each  byte contains the
  2041.                  number of bytes of object code in the corresponding source
  2042.                  line.   This appears to be an array of SHORTINT since if a
  2043.                  "line" contains more than 127 bytes, then a single byte of
  2044.                  $80 precedes  the actual  byte count as a sort of "escape"
  2045.                  and the  next byte  records the  up to  255 bytes  for the                                                                              |
  2046.                  line.  This situation has not yet been fully explored.  We                                                                              |
  2047.                  do not  yet know  what happens  in the  event  a  line  is                                                                              |
  2048.                  credited with spawning more than 255 bytes of code.                                                                              |
  2049.  
  2050.  
  2051.  
  2052.  
  2053.  
  2054.  
  2055.  
  2056.  
  2057.  
  2058.  
  2059.  
  2060.  
  2061.  
  2062.  
  2063.  
  2064.  
  2065.  
  2066.  
  2067.        ----------------------------------------------------------------------
  2068.        Rev: August 11, 1990                                           Page 31
  2069.  
  2070.  
  2071.  
  2072.                            Inside TURBO Pascal 5.5 Units           
  2073.        ----------------------------------------------------------------------
  2074.  
  2075.        6. CODE, DATA, RELOCATION INFO
  2076.  
  2077.  
  2078.        This area  begins at the start of the next free PARAGRAPH.  This means
  2079.        that its  offset from  the beginning  of the  Unit ALWAYS  ends in the
  2080.        digit zero.
  2081.  
  2082.        This area  contains the  CODE segments,  CONST DATA  segments, and the
  2083.        Relocation Data required for linking.
  2084.  
  2085.  
  2086.  
  2087.        6.1 OBJECT CSEGS
  2088.  
  2089.  
  2090.        Each CODE  segment included  in the  unit appears here as specified by
  2091.        the CSeg  Map Table.  Depending on usage, these segments may appear in
  2092.        the executable file.  There are no filler bytes between segments.
  2093.  
  2094.  
  2095.  
  2096.        6.2 CONST DSEGS
  2097.  
  2098.  
  2099.        This section begins at the start of the first free PARAGRAPH following
  2100.        the end  of the  Object CSegs.   This  means that  its offset from the
  2101.        beginning of the Unit ALWAYS ends in the digit zero.
  2102.  
  2103.        A DATA  segment fragment  appears here  for each  CSeg that declares a
  2104.        typed constant,  and for  each OBJECT  which employs  Virtual Methods.
  2105.        There are no filler bytes between segments.
  2106.  
  2107.        If local symbols were generated, there is always enough information to
  2108.        allow documenting the scope of the declaration as well as interpreting
  2109.        the data  in the display since the needed type declarations would also
  2110.        be available.  Our program doesn't go to this extreme however.
  2111.  
  2112.  
  2113.  
  2114.  
  2115.  
  2116.  
  2117.  
  2118.  
  2119.  
  2120.  
  2121.  
  2122.  
  2123.  
  2124.  
  2125.  
  2126.  
  2127.  
  2128.  
  2129.  
  2130.  
  2131.        ----------------------------------------------------------------------
  2132.        Rev: August 11, 1990                                           Page 32
  2133.  
  2134.  
  2135.  
  2136.                            Inside TURBO Pascal 5.5 Units           
  2137.        ----------------------------------------------------------------------
  2138.  
  2139.        6.3 RELOCATION DATA TABLE
  2140.  
  2141.  
  2142.        This table  begins at  the start of the first free PARAGRAPH following
  2143.        the end  of the  CONST DSegs.   This  means that  its offset  from the
  2144.        beginning of  the Unit  ALWAYS ends  in the digit zero.  There are two                                                                              |
  2145.        sections in  this table:   one  for code,  and one  for  data.    Both                                                                              |
  2146.        sections are  aligned on  paragraph boundaries.   This may result in a                                                                              |
  2147.        "slack" entry  between the  code and data sub-sections, but this entry                                                                              |
  2148.        is included  in the  byte tally  for the  section stored  in the  Unit                                                                              |
  2149.        Header Table at ULPtch (offset +20).                                                                              |
  2150.  
  2151.        The table  begins with  entries for the CSeg Map and ends with entries
  2152.        for the  CONST DSeg  Map.   The appropriate  Map entry  specifies  the
  2153.        number of  bytes of  Relocation Data  for the  corresponding  segment.
  2154.        This number  may be zero in which case there is no Relocation Data for
  2155.        the given segment.                                                                              |
  2156.  
  2157.        The Table  consists of an array of eight (8) byte entries whose format
  2158.        is as follows:
  2159.  
  2160.          +00:    A Byte containing the offset within the Donor Unit List of
  2161.                  the Unit  name that this entry refers to.  This can be the
  2162.                  compiled Unit or some previously compiled external unit.
  2163.  
  2164.          +01:    A Byte  that defines  the type of reference being made and
  2165.                  implies the  size of  the pointer  needed (WORD or DWORD).
  2166.                  The known and/or observed values are as follows:
  2167.  
  2168.                  00h -> a WORD refers to a PROC Map.     
  2169.                  10h -> a WORD refers to a PROC Map.     
  2170.                  20h -> a WORD refers to a PROC Map.     
  2171.                  30h -> a DWORD pointer refers to a PROC Map. 
  2172.                  50h -> a WORD refers to a CSeg Map.     
  2173.                  60h -> a WORD refers to an unknown Map. 
  2174.                  70h -> a DWORD pointer refers to a CSeg Map. 
  2175.                  90h -> a WORD refers to a VAR DSeg Map. 
  2176.                  A0h -> a WORD refers to a DSeg Map for SEG address.                                                                                  |
  2177.                  D0h -> a WORD refers to a CONST DSeg Map.
  2178.  
  2179.          +02:    A  Word   containing  the  offset  within  the  Map  table
  2180.                  referenced according to the above code scheme.
  2181.  
  2182.          +04:    A Word  containing an  offset within  the  target  segment
  2183.                  which will  be  added  to  the  effective  address.    For
  2184.                  example, a  reference to  the VAR  DSeg Map will require a
  2185.                  final offset to locate the item (variable) within the DATA
  2186.                  SEGMENT being  referenced here.   This  may also be needed
  2187.                  for references to LITERAL DATA embedded in a CODE SEGMENT.
  2188.  
  2189.          +06:    A Word  containing the  offset within  the  CODE  or  DATA
  2190.                  segment owning  this entry  that contains  the area  to be                                                                              |
  2191.                  patched with the value of the final effective address.                                                                              |
  2192.  
  2193.  
  2194.  
  2195.        ----------------------------------------------------------------------
  2196.        Rev: August 11, 1990                                           Page 33
  2197.  
  2198.  
  2199.  
  2200.                            Inside TURBO Pascal 5.5 Units           
  2201.        ----------------------------------------------------------------------
  2202.  
  2203.        For some  truly wild guessing about the flag byte above, the following                                                                              |
  2204.        pattern seems  to be  emerging.   Look at  bits 7-4  of this byte.  It                                                                              |
  2205.        appears that  the type of Map reference may be coded into bits 7-6 and                                                                              |
  2206.        that the  size or  type of reference may be coded into bits 5-4.  Note                                                                              |
  2207.        that bits  7-6 are  "00" for  PROC Map items, "01" for CSeg Map items,                                                                              |
  2208.        "10" for Global DSeg Map items, and "11" for Const DSeg Map items.  It                                                                              |
  2209.        appears that the size or type of reference may be coded into bits 5-4.                                                                              |
  2210.        Note that  all FAR  (DWORD) pointer references show these bits as "11"                                                                              |
  2211.        and that a SEGMENT Register value appears as "10" and that WORD values                                                                              |
  2212.        otherwise appear  as "01" or "00".  Further, no type 00h item has been                                                                              |
  2213.        seen which  has a  non-zero effective  address adjustment.   This  all                                                                              |
  2214.        seems to suggest the following code structure:                                                                              |
  2215.  
  2216.          7654 3210    (bits 3-0 don't seem to be used)                                                                              |
  2217.  
  2218.          00-- ----    Locate item via a PROC Map,                                                                              |
  2219.          01-- ----    Locate item via a CSeg Map,                                                                              |
  2220.          10-- ----    Locate item via a Global DSeg Map,                                                                              |
  2221.          11-- ----    Locate item via a Const  DSeg Map,                                                                              |
  2222.          --00 ----    WORD offset has NO effective address adjustment,                                                                              |
  2223.          --01 ----    WORD offset HAS an effective address adjustment,                                                                              |
  2224.          --10 ----    WORD is content of a SEGMENT Register such as DS                                                                              |
  2225.                       or CS.                                                                              |
  2226.          --11 ----    DWORD (FAR) pointer is supplied with possible                                                                              |
  2227.                       effective address adjustment.                                                                              |
  2228.  
  2229.        The evidence  in support of this conjecture is both slim and vast.  It                                                                              |
  2230.        all depends  on how much data one looks at.  I have looked at a lot of                                                                              |
  2231.        data from  the Borland  supplied units and I haven't found anything to                                                                              |
  2232.        refute the  above.   Accordingly, the supplied program interprets this                                                                              |
  2233.        flag byte according to this scheme.                                                                              |
  2234.  
  2235.  
  2236.  
  2237.        7. SUPPLIED PROGRAM
  2238.  
  2239.  
  2240.        In order that the above information be made constructively useful, the
  2241.        author has designed a program that automates the process of discovery.
  2242.        It is  not a  "handsome" program and it is not a work of art.  It does
  2243.        give useful results provided your PC has enough available memory.
  2244.  
  2245.        It should  be obvious  that the  program was  not designed "top-down".
  2246.        Rather, it  just evolved as each new discovery was made.  Later on, it
  2247.        seemed reasonable to try to document some of the relations between the
  2248.        various lists  and tables  and the program tries to make some of these
  2249.        relations clear, albeit with varying degrees of success.
  2250.  
  2251.  
  2252.  
  2253.  
  2254.  
  2255.  
  2256.  
  2257.  
  2258.  
  2259.        ----------------------------------------------------------------------
  2260.        Rev: August 11, 1990                                           Page 34
  2261.  
  2262.  
  2263.  
  2264.                            Inside TURBO Pascal 5.5 Units           
  2265.        ----------------------------------------------------------------------
  2266.  
  2267.        7.1 TPUNEW                                                                              |
  2268.  
  2269.  
  2270.        This is  the main program.  It will ask for the name of the unit to be
  2271.        documented.   Reply with  the unit name only.  The program will append
  2272.        the ".TPU" extension and will search for the proper file.
  2273.  
  2274.        The program  will then ask if Dis-Assembly is desired and will require
  2275.        a "y" or "n" answer.
  2276.  
  2277.        The  current  directory  will  be  searched  first,  followed  by  all
  2278.        directories in the current PATH.  The program will NOT search a ".TPL"
  2279.        (Turbo Pascal Library) file.
  2280.  
  2281.        If the  desired unit  is found, the program will write a report to the
  2282.        current directory  named "unitname.lst"  which contains  its analysis.
  2283.        The format of the report is such that it may be copied to a printer if
  2284.        that printer supports TTY control codes with form-feeds.  Be judicious
  2285.        in doing  this however  since there  can be a lot of information.  The
  2286.        Turbo SYSTEM.TPU  unit file  produces almost ninety (90) pages without                                                                              |
  2287.        the disassembly  option.  When disassembly is requested for the SYSTEM                                                                              |
  2288.        unit, the size of the output file exceeds 700K bytes.                                                                              |
  2289.  
  2290.  
  2291.  
  2292.        7.2 TPURPT1
  2293.  
  2294.  
  2295.        This is  a Unit that contains the text-file output primitives required
  2296.        by the main program.  It's not very pretty but it does work.
  2297.  
  2298.  
  2299.  
  2300.        7.3 TPUAMS1
  2301.  
  2302.  
  2303.        This Unit  contains all  Type Definitions,  Structures,  and  "Canned"
  2304.        Functions and Procedures required by the main program.  All structures
  2305.        documented in  this report  are also documented in TPUAMS1 by means of
  2306.        the TYPE  mechanism.   Some of  the structures  are difficult  if  not
  2307.        impossible to  handle using  ISO Pascal  but Turbo Pascal provides the
  2308.        means for getting the job done.
  2309.  
  2310.  
  2311.  
  2312.        7.4 TPUUNA1
  2313.  
  2314.  
  2315.        This unit is a rudimentary disassembler.  The output will not assemble
  2316.        and may  look strange to a real assembler programmer since this author
  2317.        is not  so-qualified.   However, the basis for support of 80286, 80386
  2318.        etc. processors is present as well as coprocessor support.  Of perhaps
  2319.        the greatest  interest is  that it  does appear to decode the emulated
  2320.        coprocessor instructions that are implemented via INT 34-3D.
  2321.  
  2322.  
  2323.        ----------------------------------------------------------------------
  2324.        Rev: August 11, 1990                                           Page 35
  2325.  
  2326.  
  2327.  
  2328.                            Inside TURBO Pascal 5.5 Units           
  2329.        ----------------------------------------------------------------------
  2330.  
  2331.        Be warned  however.  The output is not guaranteed since this was coded
  2332.        by myself  and I  am perhaps  the rankest amateur that ever approached
  2333.        this quite  awful assembler  language.   For convenience,  the operand
  2334.        coding mimics TASM "Ideal" mode.
  2335.  
  2336.        As is  usual with programs of this type, error-recovery is minimal and
  2337.        no context  checking is  performed.  If the operation code is found to
  2338.        be valid,  then a  valid instruction  is assumed  -- even  if  invalid
  2339.        operands are present.
  2340.  
  2341.        The only positives that apply to this program are that it doesn't slow
  2342.        the cpu down (although a lot more output is produced), and it does let
  2343.        one "tune" code for compactness by letting one view the results of the
  2344.        coding directly.   Also,  incomplete instructions  are handled as data                                                                              |
  2345.        rather than overrunning into the next proc.                                                                              |
  2346.  
  2347.  
  2348.  
  2349.        7.5 MODIFICATIONS
  2350.  
  2351.  
  2352.        It was intended from the beginning that this program should be able to
  2353.        be enhanced  to permit  external units  to be  referenced  during  the
  2354.        analysis of any given unit, even if they were library components.  The
  2355.        author hopes  that users so-inclined will find the code pliable enough
  2356.        to engineer  such enhancements.   No small amount of care was expended
  2357.        to make  pointer references flexible enough so that more than one unit
  2358.        could be  addressed at  one time.   However, none of the references to
  2359.        external units are resolved by the program as it now stands.
  2360.  
  2361.        This program  was NOT intended as a pilot for some future product.  It                                                                              |
  2362.        WAS intended as a rather "ersatz" tool for myself.                                                                              |
  2363.  
  2364.  
  2365.  
  2366.        7.6 NOTES ON PROGRAM LOGIC                                                                              |
  2367.  
  2368.  
  2369.        The following  sections discuss  a few  of the methods employed by the
  2370.        supplied program.
  2371.  
  2372.  
  2373.  
  2374.  
  2375.  
  2376.  
  2377.  
  2378.  
  2379.  
  2380.  
  2381.  
  2382.  
  2383.  
  2384.  
  2385.  
  2386.  
  2387.        ----------------------------------------------------------------------
  2388.        Rev: August 11, 1990                                           Page 36
  2389.  
  2390.  
  2391.  
  2392.                            Inside TURBO Pascal 5.5 Units           
  2393.        ----------------------------------------------------------------------
  2394.  
  2395.        7.6.1 FORMATTING THE DICTIONARY                                                                              |
  2396.  
  2397.  
  2398.        Printing the unit dictionary area in a way that exposes its underlying                                                                              |
  2399.        semantics is  no small  task.   The unit  dictionary area  itself is a                                                                              |
  2400.        rather  amorphous-looking  mass  of  data  composed  of  hash  tables,                                                                              |
  2401.        dictionary headers  and stubs,  type descriptors,  etc.   In order  to                                                                              |
  2402.        present all  this information  in a  meaningful way, we have to reveal                                                                              |
  2403.        its structure  and this  cannot be  done  by  means  of  a  sequential                                                                              |
  2404.        "browse" technique.   Rather,  we have  to  visit  all  nodes  in  the                                                                              |
  2405.        dictionary area  so that  each may  be formatted in a way that exposes                                                                              |
  2406.        their function  and meaning.   This is made necessary by the fact that                                                                              |
  2407.        items are  added to  the dictionary  as encountered  and no convenient                                                                              |
  2408.        ordering of  entry types  exists.  What we have here is the problem of                                                                              |
  2409.        finding a  minimal "cover"  for  the  dictionary  area  that  properly                                                                              |
  2410.        exposes the content and structure of the dictionary area.                                                                              |
  2411.  
  2412.        To do  this, we  construct (in  the heap) a stack and a queue, both of                                                                              |
  2413.        which are  initially empty.   The entries we put in the stack identify                                                                              |
  2414.        the class  of entry (Hash Table, Dictionary Header, Type Descriptor or                                                                              |
  2415.        In-Line Code  group), the  location of the structure, and the location                                                                              |
  2416.        of its  immediate "owner"  or "parent"  dictionary entry (which allows                                                                              |
  2417.        some limited information about scope to be printed).                                                                              |
  2418.  
  2419.        To the  empty stack,  we add  an entry  for the  unit name  dictionary                                                                              |
  2420.        entry, the  INTERFACE hash table, and the DEBUG hash table.  All these                                                                              |
  2421.        are located  via direct  pointers (LL's) in the Unit Header Table.  We                                                                              |
  2422.        then pop one entry off the stack and begin our analysis.                                                                              |
  2423.  
  2424.        a)    If the  entry we  popped off  the stack  is not  present in  the                                                                              |
  2425.              queue, we add it and call a routine that can interpret the entry                                                                              |
  2426.              (aka, "cover")  for a  Dictionary Header,  Hash Table,  or  Type                                                                              |
  2427.              Descriptor.  (This may lead to additional entries being added to                                                                              |
  2428.              the stack  such as nested-scope hash tables, Dictionary Headers,                                                                              |
  2429.              Type Descriptors or In-Line Code group entries.)                                                                              |
  2430.  
  2431.        b)    While the  stack is  not empty,  we pop another entry and repeat                                                                              |
  2432.              step "a" (above) until no more entries are available.                                                                              |
  2433.  
  2434.        The result  is a  queue containing one entry for each structure in the                                                                              |
  2435.        unit  dictionary  area  that  is  identifiable  via  traversal.    (In                                                                              |
  2436.        practice, the  method we use is similar to a "breadth-first" traversal                                                                              |
  2437.        of an  n-way tree that is implemented in non-recursive fashion.)  Each                                                                              |
  2438.        entry in  the queue  contains the  information described above and the                                                                              |
  2439.        queue itself thus forms a set of descriptors that drive the process of                                                                              |
  2440.        formatting the  dictionary area  for display.    The  process  may  be                                                                              |
  2441.        likened to  "painting by  the numbers" or to finding a way to lay tile                                                                              |
  2442.        on a flat surface using tiles of four different irregular shapes until                                                                              |
  2443.        the floor is exactly covered.                                                                              |
  2444.  
  2445.        There is  one significant limitation that needs to be pointed out.  It                                                                              |
  2446.        is not  always possible to determine the "parent" or "owner" of a node                                                                              |
  2447.        with certainty.   The  following discussion illustrates the problem of                                                                              |
  2448.        finding the "real" parent of a Type Descriptor.                                                                              |
  2449.  
  2450.  
  2451.        ----------------------------------------------------------------------
  2452.        Rev: August 11, 1990                                           Page 37
  2453.  
  2454.  
  2455.  
  2456.                            Inside TURBO Pascal 5.5 Units           
  2457.        ----------------------------------------------------------------------
  2458.  
  2459.        Almost every "type" in Pascal is actually derived from the basic types                                                                              |
  2460.        that are  (in Turbo  Pascal) defined  in the  SYSTEM.TPU unit  -- e.g.                                                                              |
  2461.        "INTEGER", "BYTE",  etc.  In addition, several of the Type Descriptors                                                                              |
  2462.        in the  SYSTEM unit  are referenced by more than one Dictionary Entry.                                                                              |
  2463.        Thus, we  find that  a "many-to-one"  relationship may  exist  between                                                                              |
  2464.        Dictionary Entries  and Type Descriptors.  How does one find out which                                                                              |
  2465.        is the entry that actually gave rise to the Type Descriptor?                                                                              |
  2466.  
  2467.        The Dictionary  Area of  a unit  has some  special properties,  one of                                                                              |
  2468.        which is  the fact  that the  Dictionary Entries  for named  Types are                                                                              |
  2469.        often  located  quite  near  their  primary  type  descriptors.    The                                                                              |
  2470.        Dictionary Area seems to be treated as an upward growing heap with the                                                                              |
  2471.        various structures  being added  by Turbo  as needed.   This  makes it                                                                              |
  2472.        likely that  the Type "Q" header which gives rise to a type descriptor                                                                              |
  2473.        is quite likely to occur earlier in the Dictionary Area than any other                                                                              |
  2474.        header which refers to the same descriptor.  We take advantage of this                                                                              |
  2475.        property to allocate "ownership" but it may not be "fool-proof".  Some                                                                              |
  2476.        type descriptors are spawned by other type descriptors, especially for                                                                              |
  2477.        structured types.   We  don't attempt to allocate "ownership" to these                                                                              |
  2478.        "lower-level" descriptors.                                                                              |
  2479.  
  2480.  
  2481.  
  2482.        7.6.2 THE DISASSEMBLER                                                                              |
  2483.  
  2484.  
  2485.        To start with, I apologize up front for mistakes which are bound to be                                                                              |
  2486.        present in  this routine.   I  am not  a MASM or TASM programmer and I                                                                              |
  2487.        will not  pretend otherwise.   This  being the  case, the formatting I                                                                              |
  2488.        have chosen  for the operands may be erroneous or misleading and might                                                                              |
  2489.        (if submitted  to one  of the  "real" assemblers)  produce object code                                                                              |
  2490.        quite different  from what  is expected.   I  hope not,  but I have to                                                                              |
  2491.        admit it's possible.                                                                              |
  2492.  
  2493.        My intention  in adding  this unit was to permit tuning of object code                                                                              |
  2494.        to be  made possible.   With practice and some effort, one can observe                                                                              |
  2495.        the effect  on the  object module  caused by  specific Pascal  coding.                                                                              |
  2496.        Thus, where  compactness is  an issue of paramount importance, TPUUNA1                                                                              |
  2497.        can be  of help.   In some cases, a simple re-arrangement of the local                                                                              |
  2498.        variable declarations  in a procedure can have a significant effect of                                                                              |
  2499.        the size  of the  code if it means the difference between 1 and 2-byte                                                                              |
  2500.        displacements for  each instruction  that references  a specific local                                                                              |
  2501.        variable.   Potential  applications  along  these  lines  seem  almost                                                                              |
  2502.        unlimited.                                                                              |
  2503.  
  2504.  
  2505.  
  2506.  
  2507.  
  2508.  
  2509.  
  2510.  
  2511.  
  2512.  
  2513.  
  2514.  
  2515.        ----------------------------------------------------------------------
  2516.        Rev: August 11, 1990                                           Page 38
  2517.  
  2518.  
  2519.  
  2520.                            Inside TURBO Pascal 5.5 Units           
  2521.        ----------------------------------------------------------------------
  2522.  
  2523.        I adopted an operand format not unlike that of TASM "Ideal" mode since                                                                              |
  2524.        it was  more convenient  to do  so and  looked more readable to me.  I                                                                              |
  2525.        relied on  several reference books for guidance in decoding the entire                                                                              |
  2526.        mess and  I found  that there were several flaws (read ERRORS) in some                                                                              |
  2527.        of them  which made  the  job  that  much  more  difficult.    I  then                                                                              |
  2528.        compounded my  problems  by  attempting  to  handle  80286  and  80386                                                                              |
  2529.        specific code even though Turbo Pascal does not generate code specific                                                                              |
  2530.        to these  processors.   I simply  felt that  the  effort  involved  in                                                                              |
  2531.        writing any sort of Dis-Assembly program for Turbo Pascal units was an                                                                              |
  2532.        effort best  experienced not  more than  once.   With all  this  self-                                                                              |
  2533.        flagellation out of my system once and for all, I will try to show the                                                                              |
  2534.        basic strategy  of the program and to explain the limitations and some                                                                              |
  2535.        of the discoveries I made.                                                                              |
  2536.  
  2537.        The routine  is intended  to be  idiotically simple - i.e., no smarter                                                                              |
  2538.        than the  DEBUG command  in principle.   The basic idea is:  pass some                                                                              |
  2539.        text to  the routine and get back ONE line derived from some prefix of                                                                              |
  2540.        that text.   Repeat  as necessary until all text is gone.  Thus, there                                                                              |
  2541.        is no attempt to check the context of the text being processed.  Also,                                                                              |
  2542.        some configurations  of the  "modR/M" byte  may invalid  for  selected                                                                              |
  2543.        instructions.  I don't try to screen these out since the intent was to                                                                              |
  2544.        look at  the presumably  correct code  produced by TURBO Pascal -- not                                                                              |
  2545.        devious assembly language.  Also, this program regards WAIT operations                                                                              |
  2546.        as "stand-alone"  -- i.e.,  it doesn't  check to  see if a coprocessor                                                                              |
  2547.        operation follows for which the WAIT might be regarded as a prefix.                                                                              |
  2548.  
  2549.        One area  of real  difficulty  was  figuring  out  the  Floating-Point                                                                              |
  2550.        emulations used  by Turbo  Pascal that  are implemented  by  means  of                                                                              |
  2551.        interrupts $34  through $3D.   I don't know if I got it right, but the                                                                              |
  2552.        results seem reasonable and consistent.  In the listing, the Interrupt                                                                              |
  2553.        is produced  on one line, followed by its parameters on the next line.                                                                              |
  2554.        The parameter line is given the op-code "EMU_xxxx" where "xxxx" is the                                                                              |
  2555.        coprocessor op-code  I felt  was being  emulated.  Interrupt $3C was a                                                                              |
  2556.        real puzzler  but after  seeing a lot of code in context, I think that                                                                              |
  2557.        the segment  override is  communicated to the emulator by means of the                                                                              |
  2558.        first byte after the $3C.                                                                              |
  2559.  
  2560.        Normally, in  a non-emulator  environment, all  coprocessor operations                                                                              |
  2561.        (ignoring any  WAIT prefixes)  begin with  $D8-$DF.  What Borland (and                                                                              |
  2562.        maybe Microsoft)  seem to  have done  here is to change the $D8-$DF so                                                                              |
  2563.        that bits  7 and 6 of this byte are replaced with the one's complement                                                                              |
  2564.        of  the   2-bit  segment   register  number   found  in  various  8086                                                                              |
  2565.        instructions.  This seems to be how an override for the DS register is                                                                              |
  2566.        passed to  the emulator.    I  don't  KNOW  this  to  be  the  correct                                                                              |
  2567.        interpretation, but  the code I have examined in context seems to work                                                                              |
  2568.        under this  scheme,  so  TPUUNA  uses  it  to  interpret  the  operand                                                                              |
  2569.        accordingly.                                                                              |
  2570.  
  2571.        For 80x86  machines, the problem was somewhat simpler.  TPUUNA takes a                                                                              |
  2572.        quick look at the first byte of the text.  Almost any byte is valid as                                                                              |
  2573.        the initial byte of an instruction, but some instructions require more                                                                              |
  2574.        than one  byte to  hold the  complete operation  code.   Thus, step  1                                                                              |
  2575.        classifies bytes in several ways that lead to efficient recognition of                                                                              |
  2576.        valid operation codes.                                                                              |
  2577.  
  2578.  
  2579.        ----------------------------------------------------------------------
  2580.        Rev: August 11, 1990                                           Page 39
  2581.  
  2582.  
  2583.  
  2584.                            Inside TURBO Pascal 5.5 Units           
  2585.        ----------------------------------------------------------------------
  2586.  
  2587.        Once the  instruction has  been identified  in this way, it is more or                                                                              |
  2588.        less easy  to link  to supplemental  information that provides operand                                                                              |
  2589.        editing guidance, etc.                                                                              |
  2590.  
  2591.        The tables  that embody  the recognition scheme were constructed using                                                                              |
  2592.        PARADOX 3.0  (another fine Borland product) and suitably coded queries                                                                              |
  2593.        were used to generate the actual Turbo Pascal code for compilation.                                                                              |
  2594.  
  2595.        For those  that are  interested, TPUUNA  supports the address-size and                                                                              |
  2596.        operand-size prefixes  of the  80386 as  well as  32-bit operands  and                                                                              |
  2597.        addresses but  remember that  Turbo Pascal  doesn't generate these.  A                                                                              |
  2598.        trivial change  is provided for which allows segments which default to                                                                              |
  2599.        32-bit mode to be handled as well.                                                                              |
  2600.  
  2601.        There is  a simple  mode variable  that gets  passed to  TPUUNA by its                                                                              |
  2602.        caller which  specifies the most-capable processor whose code is to be                                                                              |
  2603.        handled.   Codes are  provided for  the 8086 (8088 is the same), 80186                                                                              |
  2604.        (same as  80286 except  no protected  mode instructions), 80286 (80186                                                                              |
  2605.        plus protected mode operation), and 80386.                                                                              |
  2606.  
  2607.        No such  specifier is provided for coprocessor support.  What is there                                                                              |
  2608.        is what  I think an 80387 supports.  I don't think that this is really                                                                              |
  2609.        a problem if you don't try to use TPUUNA for anything but Turbo Pascal                                                                              |
  2610.        code.                                                                              |
  2611.  
  2612.        Error recovery is predictably simple.  The initial text byte is output                                                                              |
  2613.        as the  operand of a DB pseudo-op and provision is made to resume work                                                                              |
  2614.        at the next byte of text.                                                                              |
  2615.  
  2616.        I hope  this program  is found  to be useful in spite of the errors it                                                                              |
  2617.        must surely  contain.   I have yet to make much sense of the rules for                                                                              |
  2618.        MASM or  TASM operand  coding and I found very little of value in many                                                                              |
  2619.        of the  so-called "texts"  on the  subject.   I found  myself  in  the                                                                              |
  2620.        position of  that legendary  American  watching  a  Cricket  match  in                                                                              |
  2621.        England for the first time ("You mean it has RULES?").                                                                              |
  2622.  
  2623.  
  2624.  
  2625.  
  2626.  
  2627.  
  2628.  
  2629.  
  2630.  
  2631.  
  2632.  
  2633.  
  2634.  
  2635.  
  2636.  
  2637.  
  2638.  
  2639.  
  2640.  
  2641.  
  2642.  
  2643.        ----------------------------------------------------------------------
  2644.        Rev: August 11, 1990                                           Page 40
  2645.  
  2646.  
  2647.  
  2648.                            Inside TURBO Pascal 5.5 Units           
  2649.        ----------------------------------------------------------------------
  2650.  
  2651.        8. UNIT LIBRARIES
  2652.  
  2653.  
  2654.        This author  has examined  .TPL files  in passing  and concludes  that
  2655.        their structure is trivial in the extreme.  The following notes should
  2656.        be of some help.
  2657.  
  2658.  
  2659.  
  2660.        8.1 LIBRARY STRUCTURE
  2661.  
  2662.  
  2663.        A Turbo  Pascal Library  (.TPL) file appears to be a simple catenation
  2664.        of Turbo  Pascal Unit (.TPU) files.  Since the length of a Unit may be
  2665.        determined from the Unit Header (see section 3.2), it is simple to see
  2666.        that one may "browse" through a .TPL file looking for an external unit
  2667.        such as  SYSTEM.TPU.   If this seems to be too much effort, then there
  2668.        is always the TPUMOVER Utility program supplied by Borland.
  2669.  
  2670.  
  2671.  
  2672.        8.2 THE TPUMOVER UTILITY
  2673.  
  2674.  
  2675.        Quite simply, this Utility allows one to extract units from .TPL files
  2676.        in order  to subject  them to the analysis performed by TPUMAIN.  Read
  2677.        your Turbo  Pascal User's  Guide for instructions on the operation and
  2678.        use of this utility.
  2679.  
  2680.  
  2681.  
  2682.        9. APPLICATION NOTES
  2683.  
  2684.  
  2685.        One of the more obvious applications of this information would seem to
  2686.        be in the area of a Cross-Reference Generator.
  2687.  
  2688.        There is  a very  fine example  of such a program in the public domain
  2689.        that was  written by  Mr. R. N. Wisan called  "PXL".  This program has
  2690.        been around since the days of Turbo Pascal Version 1.  The program has
  2691.        been continually enhanced by the author in the way of features and for
  2692.        support of the newer Turbo Pascal versions.  It does not however solve
  2693.        the problem  of telling  one which  unit contains  the definition of a
  2694.        given symbol.   In fairness to "PXL" however, this is no small problem
  2695.        since the  format of  .TPU files  keeps changing  (Turbo 5.5 Units are
  2696.        not object-code  compatible with  Turbo 5.0  Units, and  so on...) and
  2697.        Mr. Wisan probably has more than enough other projects to keep himself
  2698.        occupied.
  2699.  
  2700.        However, for  the user who is willing to work a little (maybe a lot?),
  2701.        this document would seem to provide the information needed to add such
  2702.        a function to his own pet cross-reference generator.
  2703.  
  2704.  
  2705.  
  2706.  
  2707.        ----------------------------------------------------------------------
  2708.        Rev: August 11, 1990                                           Page 41
  2709.  
  2710.  
  2711.  
  2712.                            Inside TURBO Pascal 5.5 Units           
  2713.        ----------------------------------------------------------------------
  2714.  
  2715.        10. ACKNOWLEDGEMENTS
  2716.  
  2717.  
  2718.        This project  would have  been totally  infeasible without  the aid of
  2719.        some very  fine tools.  As it was, several hundred man hours have been
  2720.        expended on  it and  as you can see, there are a few unresolved issues
  2721.        that have  been (graciously)  left for  others to  address.  The tools
  2722.        used by this author consisted of:
  2723.  
  2724.        1)    Turbo Pascal 5.5 Professional by Borland International
  2725.  
  2726.        2)    Microsoft WORD (version 5.0)
  2727.  
  2728.        3)    LIST (version 6.4a) by Vernon D. Buerg
  2729.  
  2730.        4)    the DEBUG utility in MS-DOS Version 3.3.
  2731.  
  2732.        5)    PARADOX 3.0 by Borland International                                                                              |
  2733.  
  2734.        6)    QUATTRO PRO by Borland International                                                                              |
  2735.  
  2736.        7)    TURBO ASSEMBLER 1.1 by Borland International                                                                              |
  2737.  
  2738.        (PARADOX and QUATTRO PRO were used for data collection and analysis in                                                                              |
  2739.        the course of coding the recognizer tables for the disassembler unit.)                                                                              |
  2740.  
  2741.        The references  listed were of great value in this project.  [Intel85]                                                                              |
  2742.        was a valuable source of information about coprocessor instructions as                                                                              |
  2743.        well as offering hints about the differences between the 8086/8088 and                                                                              |
  2744.        the 80286.   The  [Borland] TASM  manuals offered  further info on the                                                                              |
  2745.        80186.     [Nelson]  provided  presentations  of  well-organized  data                                                                              |
  2746.        directed at the problem of disassembly but the tables were flawed by a                                                                              |
  2747.        number of  errors which  crept into my databases and which caused much                                                                              |
  2748.        of the extra debugging effort.  [Intel89] offered valuable insights on                                                                              |
  2749.        the 80386  addressing schemes  as well  as the 32-bit data extensions.                                                                              |
  2750.        Finally,  [Brown]   provided  valuable  clues  on  the  Floating-Point                                                                              |
  2751.        emulators used  by Borland  (and Microsoft?).   As  you can  see,  the                                                                              |
  2752.        amount of  hard information  available to me on this project was quite                                                                              |
  2753.        limited since I am unaware of any other existing body of literature on                                                                              |
  2754.        this subject.                                                                              |
  2755.  
  2756.        That's it  folks.   Does anyone wonder why it took several hundred man
  2757.        hours to  get to  this point?   It  took a  lot of  hard (and at times
  2758.        tedious) work  coupled with a great many lucky guesses to achieve what
  2759.        you see here.
  2760.  
  2761.  
  2762.  
  2763.  
  2764.  
  2765.  
  2766.  
  2767.  
  2768.  
  2769.  
  2770.  
  2771.        ----------------------------------------------------------------------
  2772.        Rev: August 11, 1990                                           Page 42
  2773.  
  2774.  
  2775.  
  2776.                            Inside TURBO Pascal 5.5 Units           
  2777.        ----------------------------------------------------------------------
  2778.  
  2779.        11. REFERENCES
  2780.  
  2781.  
  2782.        [Bor88a],  TURBO ASSEMBLER  REFERENCE  GUIDE,  Borland  International,                                                                              |
  2783.                   1988.                                                                              |
  2784.  
  2785.        [Bor88b],  TURBO ASSEMBLER USER'S GUIDE, Borland International, 1988.                                                                              |
  2786.  
  2787.        [Bor88c],  TURBO  PASCAL   REFERENCE  GUIDE   Version   5.0,   Borland                                                                              |
  2788.                   International, 1988.                                                                              |
  2789.  
  2790.        [Bor88d],  TURBO   PASCAL    USER'S   GUIDE   Version   5.0,   Borland                                                                              |
  2791.                   International, 1988.                                                                              |
  2792.  
  2793.        [Bor89],   TURBO PASCAL 5.5 OBJECT-ORIENTED PROGRAMMING GUIDE, Borland                                                                              |
  2794.                   International, 1989.                                                                              |
  2795.  
  2796.        [Brown],   INTER489.ARC, Ralf Brown, 1989                                                                              |
  2797.  
  2798.        [Intel85], iAPX 286  PROGRAMMER'S REFERENCE  MANUAL INCLUDING THE iAPX                                                                              |
  2799.                   286 NUMERIC  SUPPLEMENT, Intel  Corporation,  1985,  (order                                                                              |
  2800.                   number 210498-003).                                                                              |
  2801.  
  2802.        [Intel89], 386  SX MICROPROCESSOR PROGRAMMER'S REFERENCE MANUAL, Intel                                                                              |
  2803.                   Corporation, 1989, (order number 240331-001).                                                                              |
  2804.  
  2805.        [Nelson],  THE 80386  BOOK:   ASSEMBLY LANGUAGE PROGRAMMER'S GUIDE FOR                                                                              |
  2806.                   THE 80386, Ross P. Nelson, Microsoft Press, 1988.                                                                              |
  2807.  
  2808.        [Scanlon], 80286  ASSEMBLY   LANGUAGE  ON  MS-DOS  COMPUTERS,  Leo  J.                                                                              |
  2809.                   Scanlon, Brady 1986.                                                                              |
  2810.  
  2811.  
  2812.  
  2813.  
  2814.  
  2815.  
  2816.  
  2817.  
  2818.  
  2819.  
  2820.  
  2821.  
  2822.  
  2823.  
  2824.  
  2825.  
  2826.  
  2827.  
  2828.  
  2829.  
  2830.  
  2831.  
  2832.  
  2833.  
  2834.  
  2835.        ----------------------------------------------------------------------
  2836.        Rev: August 11, 1990                                           Page 43